Within the WPF Designer, what does Enabling/Disabling Project Code do? - wpf

I have no clue how long this feature/option has been around but my mouse happened to pause over this button within the WPF designer early and now I'm curious what it's purpose is.
Hovering over the button, Visual Studio pops up a tooltip that says Enable project code. When I click on it, the designer seems to reload the xaml contents but I can't tell what's changed.
What does this feature do?
2020 EDIT
It's been years since I've had to work in XAML, but I've received a new project where I need to work in XAML. This feature/button seems to have disappeared. Where has it migrated?

What does this feature do?
It enables or prevents the designer from running your code in the background.
Disabling project code can lead to a loss of design time data and enabling it can lead to unhandled exceptions in the XAML designer.
These exceptions are usually caused by code that attempts to access properties or methods which work differently when your application is running in the designer compared to when it runs as a built executable.
Debugging or Disabling Project Code in XAML Designer: https://msdn.microsoft.com/en-us/library/mt622752.aspx?f=255&MSPPError=-2147217396

Related

Visual Studio toolbox custom controls only visible once

I know there are a lot of problems like that and you all might think this is a duplicate but NO... this problem is different.
When I add a custom user control to a project and rebuild it, the control is added to the toolbox. If I try to add another one it does not show up in the toolbox. If I close visual studio and reopen it the first control also was removed from the toolbox and will never appear again. From now on the project is broken and no control added, remove + readded will ever show up in the toolbox. So I create a new project and it starts from the beginning.
Before this tip appears I'll say it: Automatically Populate Toolbox is True. Nothing helps against this problem. Build Project/Solution, Rebuild, Clean+Rebuild, Restart of VS, Restart of Computer, Unload and reload the project, set toolbox back to default. Nothing works.
Any ideas??
You have to remember that the designer call your code constructor and some other methods at design time.
If you build the code and the control does not appears and it has not been hidden by an attribute, then probably the code you wrote is not compatible with the designer. For example, the control might throw an exception in design mode because of incorrect code.
The attribute is [ToolboxItem(false)]. If you have a base class that hide it, it will also be hidden in derived class if not specified otherwise.
You will have the same problem if that control is used in another component. You won't see it if it does not compile and run correctly.
The trick to debug that kind of problem, is to attach a debugger from a second instance of Visual Studio to find where in your code you throw an exception.
If the control does not work at run-time, better to fix that first as if it also fix design problem, it is simpler to debug that way.
Otherwise, you have to ensure that your project dependencies are correct. The toolbox will only shows component from DLLs that are referenced by the current project (or from itself I think).

System.Runtime.RemotingException in simple XAML file

I have a WPF application which is giving me a very very annoying error
System.Runtime.Remoting.RemotingException
[12068] Designer process terminated unexpectedly!
The number between [] changes each time
The XAML is very simple
<UserControl x:Class="STC.Reports.ReportGenerator.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</UserControl>
This has wasted me hours and hours
Does anyone know anything that could help?
I cant get any more information than that above
Even though I have told VS to break on these Exceptions it doesnt
I have also tried to attach another VS and that makes no difference
I have repaired my installation of VS (2012 Professional)
I have also updated to Update 3
This is even happening when I create a brand new WPF application
Everything seems fine initially, then VS just hangs and the remoting error occurs
Paul
What kind of message are You getting?
If it is a Messagebox when You try to use designer - it is a known issue since VS2010 as far as I remember. It is frequently caused by a video card or its driver, so the easiest way is trying to update your video driver or use some older version. Sometimes that help.
The other way is not to use built-in designer. As for me, I wite pure xaml and it seems to be more convenient and faster way of development. Or, if You like visual UI - You may use Blend. AFAIK it is a part of VS2012 now.
I had this error occur for all windows/controls in a project, even if they were brand new empty windows. I had a markup extension causing a design-time exception (with no indication of this from Visual Studio) and I would get this designer error if I used the markup extension in any project resource dictionaries.
Old post but might be useful for future developers who run into this kind of problem.
I had this exact behaviour. Turned out to be a problem related to my viewmodel. At runtime everything works, but at design time VS will try to load the viewmodel standalone and things got haywire. My viewmodel depended on some global object which I created elsewhere at application startup. Loading the viewmodel standalone creates a exception since the global object was missing.
Quite easy to detect actualy. Just test your viewmodel by instantiating it yourself from code. If it fails VS won't be able to run the designer as well and as you see VS unfortunately won't tell you what happened.

VS 2012 hangs on loading XAML designer

We do MVVM and use DI in our WPF project. Now we are working on VS 2012. Whenever we open the views (XAML forms) the XAML designer shows the loading message. Then whenever we try to change the XAML code panel VS hangs (While it is froze on loading message). XDesProc.exe takes about 13% of CPU (As I have 8 cores so it takes 100% of a core). When I close it, VS goes out of halt. But if you change the XAML code the scenario will be repeated again.
We didn't have any problem with VS 2010 in the same situation.
What is wrong?
A work-around is described in this thread:
Visual Studio 2012 hangs on editing XAML
To summarise you can make VS not use the XAML designer, and just use the regular source editor:
Right-click on a XAML file in the Solution Explorer
Select "Open With..."
Select "Source Code (Text) Editor"
Click on "Set as Default"
Click OK
You're done!
I have the same issue on one of my views. It is not really a solution but i noticed that, when I comment out the DataContext Property, the cpu load is gone.
Open Xaml file (cpu load is rising)
Comment out DataContext property with viewmodel binding
Kill XDescProc.exe in TaskManager
Reload Designer (no cpu load)
Try installing the latest update to Visual Studio Update 1. If after the update it still occurs submit the issue to Microsoft Connect.
Make sure you that, if you have any code in the constructor or any other code that might execute in the designer, you detect the design mode and do not execute that code.
You might be connecting to a data source and the designer might be choking on that.
See: Is there a way to check if WPF is currently executing in design mode or not?
Our computer's VS2012 has updated to Update 4, but it is still hangs on loading XAML designer..

WPF Designer Never Loads Properly

I have an WPF app, all seems to run fine with one exception - the designer rarely, if ever, loads correctly. Usually complaining about Undefined CLR namespace. and then saying it can't find a few of my local: defined controls.
Is this just a defect in the designer or Visual Studios code generation or actually systemic of something wrong with my application?
In your code can be an error which occurs only in design time. It is possible to debug it. See How to: Debug a Designer Load Failure. Shortly you need another instance of Visual Studio and attach it to the one displaying your wpf app. You can even try to follow the error to its roots with How to: Debug .NET Framework Source.
I had a similar problem and I successfully tracked it down to DataContextChanged event handler and got rid of it.

How to manually reload the Visual Studio designer for WPF

Is there any way to force the WPF designer to reload or refresh, without rebuilding the entire project or solution?
If there's an error, I get an option in the designer view to refresh the designer. But if I don't have an error, how can I get the designer to refresh after I've made a change?
I'm a little late but this is the best solution1 I've found so far: whenever the designer does stupid stuff, I just kill it's process.
Press Ctrl+Shift+Esc
Navigate to the Processes tab.
Kill XDesProc.exe
This also fixes issues for the properties window (like when it gets jammed and you can't type stuff into it).
1 This is a solution for designer issues. Your issues may also be caused by compilation problems, in which case just right click on the solution in the solution explorer, and clean it. The reason behind it is that sometimes the compilation loses synchronicity with the generated files from XAML, and cleaning the solution just deletes those intermediate files; it's like a reset so your compilation can start off with a clean slate.
To do it fast:
Comfortably it's usually the last one if sorted alphabetically.
When it is, it's almost like a ritual for me to quickly pop up the task manager, click any process, press End, Delete, Enter (done), Esc (exit task manager). Instead of restarting VS and waiting for all the loads & unloads, you can do that in 1-2 seconds.
In newer versions of Visual Studio there is an icon on the bottom of the designer to "Disable Project code". If you toggle this off and on it will reload the designer.
You can add this to the Tools menu in Visual Studio.
Once configured, use Tools..XAML Designer Restart:
Alt+T then L
I tried configuring it for Alt+T then X but this clashed with Tools..Choose ToolboX Items.
Update
These days, I prefer to just hit Ctrl+Shift+Esc to bring up the process manager, then X to skip to XDesProc.exe then Delete to kill the rogue process(es).
The Visual Studio designer attempts to keep the rendered view in sync with the XAML. That's the advertised behavior.
The first thing you should check is that there are no errors in the errors window. It may be something as simple as a missing angle bracket.
The second thing to check is whether you have any code (other than your code-behind) which needs to be compiled for the designer to render your XAML correctly. This includes any of your own datatypes that you instantiate in XAML, any custom controls you have written (e.g. MyTextBlock derived from TextBlock), or any classes directly or indirectly in support of design-time data. If so, you need to rebuild your project.
The last thing to check for is possible bugs in the designer. In spite of the advertised behavior, the designer may get out-of-sync due to bugs. In that close, close the XAML window and re-open it. Other tricks that might work are selecting the XAML tab and then the Design tab, or maximizing the XAML pane.
As far as rebuilding your application goes, you don't need to do this as a habit. You only need to recompile it when the above conditions apply. Once they don't apply, you can just edit the XAML. Another way to say this is that if you haven't modified code, you shouldn't need to rebuild (modulo bugs).
I'm not sure, but I think a build will refresh your view in that situation.
There is any event handled in that XAML file, then mostly it will not display the design preview from Visual Studio. If you want to see the design from Visual Studio, try with Command Binding instead of event, you will see the preview.
I'm not sure how this works in WPF editing, but with ASP.NET pages when the design view wont update i can do 2 things
Exit Visual Studio and restart
Go into source view (not split), type something and remove it (not by undoing, just delete or backspare) and save it. Then return to design view, usually the view has been updated then.
When you add a new row of code or a new object, XAML designer is sync but I encountered non-sync behavior when a property of an object is changed.
A tricky way is that when you change a property you only need to remove a ">" character from end of an instruction then retype it.
On the toolbar in the XAML designer, choose the "Disable project code" button to reload the designer link which stays on the right side of "Turn on snapping to snaplines".
Disable project code in the designer
If it is disabled, you can try to check the configuration manager and change processors to "Any CPU".
For projects that target ARM or X64 processors, Visual Studio cannot run project code in the designer, so the Disable project code button is disabled in the designer. Check this:
Debug or disable project code in XAML Designer
For information, I had the same issue with the XAML Designer of Visual Studio Community 2017, i.e. sometimes the designer doesn't show anything, the easiest solution is then to close the XAML file and reopen it.
I also frequently get the exception "An Unhandled Exception has occurred - Click here to reload the designer - Details: The XAML Designer has exited unexpectedly" (the click restarts the designer successfully).
Note that, in this VS version, the process of the XAML designer is not named XDesProc.exe, but UwpSurface.exe. If you prefer or have to kill the process, then the designer shows the same exception as above, and you may restart it.
use process hacker and kill the WpfSurface process (blend only)
Update for designer refresh/reload Visual Studio 2022 Xamarin, taskkill /IM java.exe

Resources