XAML UI updates not visible in code-behind - c#

Having the same project opened in both, Visual Studio and Blend, I use the former to write code while I edit the XAML in the latter.
When I add an UI element to the .xaml file and save it in Blend, if I have that file opened in Visual Studio too, I see the message that some external changes have been done and asking if I want them to take effect in the opened file*. I accept it, and I can see the changes take effect in the .xaml file in Visual Studio.
The problem is, that although the new elements have been added to the .xaml file, I'm not able to access them throug Intellisense in the code-behind .cs file.
For example if I add following element to the MainPage.xaml using Blend:
<Button x:Name="MyButton" Content="Button"/>
I can see that same line added to the page in Visual Studio, but I cannot access it using the name MyButton in MainPage.xaml.cs file. Intellisense is not working for it. If I write, for example MyButton.Content = "Hello!"; it is red underlined, however I can build and run the project.
Rebuilding the project doesn't do the trick, and Intellisense doesn't work until I save the file "again" in Visual Studio.
I feel this behavior annoying sometimes, and I'd like to know if it is the normal behavior, or there is something wrong in my configuration.
*I usually don't have it opened in Visual Studio, just to avoid that meessage being shown for each modification in Blend

I have just check your problem actually it is not a issue. everything is fine so what you have to do is save your xaml page on which you have added the button after accepting the changes that was done in blend..(do control + s on your xaml page one more time it will work fine)..sorry for my bad english..

I encountered this issue when developing Xamarin Forms in Visual Studio 2015 and somehow the problem went away after I:
1) Unload project (right-click project and select from context menu)
2) Edit .csproj (from context menu)
3) Pretend to modify and save csproj. :)
4) Reload project (from context menu)

Closing the whole solution and re opening it fix my similar issue.

Related

Disable Design View for C# Class in VS2019

My C# project displays classes in the Solution Explorer some with the Designer View and some with the normal code editor:
AND
I Would like to make them all open with the code editor!
I went in the .csproj file and I noticed that for the classes marked as designer, I get a
<SubType>Component</SubType> tag. If I remove it manually, VS automatically re-add it to the item.
How to solve it? I searched but did not found anything usefull..
EDIT 1: I forgot to mention that this is a Console Application

Visual studio 2017 c# design view fail to load after rename

I'm working on a winform app.
The design view worked well until I renamed the Form1.cs to FormMain.cs.
The program itself is ok. It can be compiled with no error and it runs well(with right form). But visual studio keep warning me that there are mistakes in FormMain.cs. It seems that FormMain class in FormMain.Designer.cs is totally ignore as it alerts "InitializeComponent(); is not defined."
And the design view goes blank as if the form was just newly-created.
IntelliSense tells me the origin(not yet renamed) Designer.cs is not found.
Now if I try to create anything on the blank form, the code is directly written in FormMain.cs.
How can I solve this?
Thanks.
Using Visual Studio 2019, I was following the C# WPF tutorial from Microsoft docs. Along the way, there is a step wherein you rename MainWindow.xaml to Greetings.xaml. When I did this, the design view was gone. Restarting Visual Studio fixes the design view. Of course, save your files before restarting.
Removing the form and adding it again to the solution fixes this issue. Here's how you can do:
Right click on the FormMain in the solution explorer and "Exclude From Project". Now right-click on the solution and click Add -> Existing Item and select the FormMain.cs from the same solution folder.

How to add a new XAML View with code behind

I am using VS 2015, creating a Univerasl App. I want to create a new view (XAML). I can right click, Add > XAML > XAML View, and the XAML gets created with the name and location that I want.
But, how can I create a code behind here, e.g. MyNewView.xaml.cs, and "link it up" as a child node in my solution explorer?
As RavingDev said:
Do not use "XAML View", instead use "Blank Page" or "User Control".
On a side note, if you want to manually create a code file and link it with anything else (i.e. Visual Studio automatically links .cs and .xaml on creation), you'll have to edit the project's XML code.
Assume you created a XAML view/page/control named MyView.xaml and a separate C# file named MyView.xaml.cs, and they're unlinked (this can also happen if you add files directly into the Solution Explorer). To link them, you will have to edit your project's internal code. First, save and quit Visual Studio. Second, find your project file (<project name>.csproj). Open it with a text editor, such as Notepad++, VS Code, or Atom (not Visual Studio). Move down the file until you see ItemGroup elements. There are a few of them, but the one that contains Compile elements is the right one. Add the following code somewhere inside that element:
<Compile Include="MyView.xaml.cs">
<DependentUpon>MyView.xaml</DepenedentUpon>
</Compile>
Do this for every file you want to link.
If everything was done correctly, you can save the file and open it back up in Visual Studio. Your files should now be linked in the Solution Explorer.

"Go To Definition" from a .xaml.cs file takes me to .g.i.cs files instead of to .xaml file in Visual Studio 2015

I am used to Visual Studio Go To Definition (or ReSharper's Ctrl + left click) in code behind on an element name defined in XAML to take me directly to XAML of that element. For some reason it's taking me to .g.i.cs file instead. Where as it's not a huge problem it is annoying as hell. This is also not specific to my project - I can create a new WPF project, add a single button to XAML and get the same result trying to go to the button's declaration in XAML from code behind file.
I tried to do the same thing in VS 2015 Professional on another computer and get the same problem.
But when I do this in Visual Studio Enterprise 2015 on my Surface, it works correctly and goes to XAML (all testing done with new WPF project) - I can't find any option anywhere that would control this.
Any ideas for this would be greatly appreciated.
Well, it seems as ReSharper actually changed the behavior - very naughty. I am happy to say that ReSharper 9.2 fixed this problem (I was using 9.1.3 initially).

"No Code Behind File"

I have been working on c# Gui for about a week without problem, all of a sudden when I added a label control I tried to access its events and saw the error "This document item has no code behind file. Add code behind file and a class definition before adding event handlers"
This is odd since the gui/methods still runs fine but it is making it very difficult to progress...any ideas? Did a file get deleted etc? Can provide more info on request, thanks for the help!
(couldn't add screen shot unfortunately yet but error is displayed in properties window/event tab)
I encountered the same error"This document item has no code behind file. Add code behind file and a class definition before adding event handlers" in my first C++ Helloworld project when I try to add a even handler for one button I added in the MainPage.xaml.
I solved the problem by restart Visual Studio 2015 and recreate a new project, then I add a even handler for the button successfully.
It's my experience and just be posted here that maybe you can use it as reference.
I ran into this, too. In my project, most UserControls were fine, but there was one group that showed the "no code-behind" message instead of properties. The .cs files did exist, though. In the Solution Explorer tree, the xaml files all had xaml.cs files. I checked the csproj file, and all the xaml.cs files were tagged as <DependentUpon> the correct matching xaml file.
I was able to fix this by closing the solution, then deleting the csproj.vspscc (Source Control Project Metadata) file for the project. When I reopened the solution, that metadata file was rebuilt and the Properties were available in Visual Studio again.

Categories