Editing XAML leads Visual Studio's Designer to crash - c#

Original Question
I'm working on a WPF application with Visual Studio 2010, using Telerik.
I have been dealing with a lot of crashes everytime I use the designer : clicking on a element, changing its position, even changing its name leads to a crash, and displays the following exception :
System.ArgumentNullException
Value cannot be null.
to System.RuntimeType.MakeGenericType(Type[] instantiation)
to Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkUtil.GetRuntimeType(Type type)
to Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkType.TryGetRuntimeType()
to Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkUtil.EnsureRuntimeType(Type type)
to Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkProvider.GetRuntimeType(Type reflectionType)
[...]
I tried the following things :
Uninstall and reinstall Telerik;
Uninstall and reinstall .NET 4.0;
Uninstall and reinstall Visual Studio.
None of these attempts worked.
This morning, I noticed that the designer didn't crashed at all, and I understood why : the designer crashes when I have opened or edited XAML.
After opening XAML, Visual Studio begin to freeze and the designer crashes everytime I try to click something. If I close Visual Studio and Build the solution (without opening XAML), everything works fine with the designer.
My guess is that something goes wrong when Visual Studio tries to "convert" XAML code to graphical elements in the designer, and only in that direction.
Question : Have you ever experimented this kind of thing ? Have you any idea of why modifiying XAML causes crashes and how to solve it ?
Thank you in advance.
New attemps done after reading answers
Debug the Visual Studio instance itself when the designer opens. The method which leads to the ArgumentNullException is GetRuntimeTime. I've been able to see the .NET code but I couldn't determine the source of the problem. See the full stack trace below :
Additionally, this is the exact line where the error occurs and the exception details. Note that the file is VSIsolationProviderService.cs and that I am able to see the source thanks to the .NET Reflector Object Browser.
Message=Value cannot be null.
Source=mscorlib
StackTrace:
to System.RuntimeType.MakeGenericType(Type[] instantiation)
InnerException: null
Finally, the Local Variables inspector at the moment of the Exception shows the following object :
End of the object :
Answers to comments :
The value of this._targetFrameworkProvider at the line where the exception occurs is below.

I guess, your best bet is to debug Visual Studio!
Run Visual Studio (instance #1) and load your solution
Run a 2nd instance of Visual studio (#2)
From instance #2 go, Debug->Attach to process->Select devenv.exe (instance #1, make sure to select Managed debugging)
Then select Debug->Exception, press "Find.." and search for System.ArgumentNull then check "Thrown"
Go to instance #1, load your view in the designer, this should trigger a break point in instance #2 and it should show you a full stack trace. This information should be enough to identify the offending control/component..

I believe the problem you are seeing is related to one of the controls you are using.
Let me first show you a way of reproducing this problem; if it is the same issue you are seeing. (I'm using VS 2013 which handles this issue a little better than vs 2010)
Firstly, I created a custom control which is a TextBox; and I have code that looks something like this.
public class CustomTextBox : TextBox
{
public string testText { get; set; }
public CustomTextBox()
: base()
{
if (string.IsNullOrEmpty(testText))
{
throw new ArgumentNullException();
}
}
}
then I put this control into the xaml
<Grid>
<local:CustomTextBox/>
</Grid>
All I'm doing is throwing an exception when a property is null when the control calls its constructor (which it will do when in the designer as the designer attempts to new it up and render it).
In VS2013 I simply have a red line underneath the control in the XAML itself, but from past experience with VS2010 this issue did cause the designer to fall over.
What I may suggest you do, is go through the XAML without the designer and take out any 3rd party/custom control's one at a time. One of these controls may be throwing an exception which can produce what you are seeing. If its one of the Telerik controls, contacting them is a option.

As previously said by Gavin, try finding the control that is the issue.
You can still edit the files in VS if you right click the .xaml file select Open With and choose Source Code (Text) Editor.
You can set it to default if you expect this to be in a lot of files, and switch it when you're done.
This solution will open .xaml files without a designer, but with (some) intellisense.

Also, I read somewhere that if you start vs as an administrator it gives you issues, try starting Visual studio not as Admin
Read This, a couple of random things you can try..

You can try the XAMLPAD to test your XAML page.
Reference : https://msdn.microsoft.com/en-us/library/vstudio/ms742398(v=vs.90).aspx
Hope this helps you.

If you think that it might be related to a component you are using, you may want to try and debug the Visual Studio Design mode and see in which scenario the ArgumentNullException is thrown or at least get the CallStack when the exception is thrown.
See this link :
How to troubleshoot and debug Visual Studio design mode errors?
I personally do it with an instance of Blend and one instance of Visual Studio instead of two instances of Visual Studio.
I would also try from a complete new project without any external dependencies to see if it could be related to your Visual Studio Install

VS2010 breaking with certain XAML is a fairly well known issue.
Have you tried adding this code to the Load Event of the offending control?
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) { return; }
What causes the VS 2010 SP1 WPF Designer to crash?

Try initializing runtimeType object with typeof(Type) as
Type runtimeType = typeof(Type)
Also evaluate the value of variables reflectionType,this,this._targetFrameworkProvider and this._targetFrameworkProvider.GetRunTimeType(reflectionType) in a watch during debugging.
This might help you.

Related

How to fix Visual Studio C# Project Properties not showing?

When I try to open ANY c#(also new project created at the moment), I can't view its properties.
I got this error(translated):
An error occurred while trying to load project properties window.
Close the window and retry.
The value cannot be null.
Parameter name: val
I tried to restore vs, also tried this method: https://social.msdn.microsoft.com/Forums/azure/en-US/3fb200e0-14cf-4684-bb7f-71b7808a1f83/project-not-loading-properly?forum=visualstudiogeneral
But none of those worked..
Solved by UPDATING (not restoring/repairing) Visual Studio to last build.
That doesn't make any sense by the way: like 2 months ago this version worked too(incl. project properties page).
I guess when Microsoft releases updates it forces you somehow to update its softwares.

Failed to load toolbox item. It will be removed from the toolbox

I have a WinForm application. I also have created my own User Control for it. Everything worked fine. Until today that I received the error message when I try to add it back to my program (I never removed it. Visual Studio did).
Failed to load toolbox item #UserControlName. It will be removed from
the toolbox.
I have it in my solution explorer but it vanishes from my toolbox after this error.
I also receive the warning that says:
'#UserControlName' is never assigned to, and will always have its
default value null.
And when I look at my designer code, that is true. But I haven't done anything with my designer code. My user control is defined there but not instantiated. How can I put it back?
Does it have anything to do with the fact that I changed my build platform from x32 to x64? If that is the case I will be surprised because the program was working fine after that change.
I was suffering from the same issue for one of our applications and could not find a solution. So I created the user and custom controls manually. After searching the web again I finally found why the designer is failing for this particular project. The answer is that project is 64 bit and Visual Studio is still does not have a 64 bit version, it is still 32 bit. So the designer could not load the controls in 64 bit applications or in class libraries. Read Microsoft's article about this.
There was an article on Visual Studio web site but it was removed. See the the answer in Visual Studio support forum.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/77e10b58-43cc-4aab-919f-888f14f99571/x64-class-library-of-user-controls?forum=csharpgeneral
I had the same issue lately. As this (nor any other answer on this site and on the internet) actually helped me, I found out the way to fix it.
Just clean the files and rebuild the solution. As simple as that.
I was having same issue but I find the solution:
Click on "Project_Name" with left mouse and click "Build" then you can add the UserControl to your WinForm.
Change to AnyCPU and rebuild the project.
Visual Studio has problems with controls being 64bit.
You don't have to necessarily insert the user-control manually. I had the same situation an there was a reason for it.
In my case, it failed because the EXE assembly was compiled to 'mixed-mode' by using CLR support type /clr. After changing it to 'managed-only' by setting type /clr:pure, it worked.
For details, see my answer here at SO.
After many hours of head scratching with this problem with a large custom control with few changes from a previous working copy in source control I copied all the code into a new control name and file and it all worked.
This was to identify the problem line(s) of code since the debugger would not co-operate. The copied control (along with heaps of support code and modules) worked okay.
So these are the very simple steps that fixed my original code
In solution explorer rename the file (I just added an s at the end)
Rebuild
Test the control can now be added to a plain form
Rename the control back to its original name
Step 4 might be optional for you but if you have source control and it's a in a library you would want to do this.
This brought the control back onto all my forms that weren't working (so far as I can tell). It seems the problem is in VS that somehow remembers it doesn't like it?
Hope this helps and I hope I find this message next time I get this when I have forgotten the fix :)
P.S. Clean, rebuild and/or re-run the solution is the old stand by for this problem but this time it was just one custom control in a whole (DLL) class of them. Hope this helps.
For me, after adding the userControl, I rebuild the application first then refreshed the items on the tool box by going to projects => Refresh project toolbox items
Right click - Rebuild Solution fixed it for me!
Mostly caused by 32 bit / 64 bit architecture. Before Visual Studio 2022 the VS built in 32 bit, therefore cannot show 64 bit components.
Solution 1:
Create a new solution configuration in Configuration Manager with name: "Debug_FormDesign" or anything else.
Set the Configuration to the above name for all projects and set the platform to "AnyCPU".
Now open all projects Compile settings step by step, select the above configuration and change the Compile -> Target CPU option to AnyCPU.
Close all opened windows.
Clean solution.
Restart VS.
Select the "Debug_FormDesign" configuration in toolbar as active.
Rebuild solution.
Open the form designer -> Should work now.
You can easily switch back to the default "Debug" configuration after finish with the GUI.
Solution 2:
Use Visual Studio 2022.
At times like these, you'll be forced to tinker with the designer code. As long as you don't make drastic changes in the designer code, you shouldn't break anything. To play it safe, re-instantiate the object where the other controls are instantiated (near the top of the page). The designer should fill in the blanks for properties and such.
This should also return the control to the toolbox.
Check in your form.designer file if there is a left-behind property with that type.
It happened to me several times.
After I removed that line and rebuilt the project, everything started working.
In my case it helped to manually include the header files of the created user controls at the top of the Form where these controls are to be used.

Getting Visual Studio to display the C# code that's causing an error

I am debugging a button in my C# code, and I get a JavaScript runtime error thrown on click from a script that I have never touched at a throwerror() line. My coworker, on the other hand, gets an error that points to the actual line in the C# code that is causing the problem. We can't seem to figure out how to get my copy of Visual Studio to throw the helpful non-JS errors.
When I debug (Clicking 'Start Debugging' with Internet Explorer selected), and I try to debug a asp.net button, I receive an error in ScriptResource.axd, like this:
And is shows me that the error is here:
Now, when my colleague does the same process to debug the same code, this is the error he receives:
We are both using IE to debug in Visual Studio. Both VS instances are 2012. I have not come across this issue and neither has my colleague. Does anyone have any insight to this?
Have you tried comparing Exceptions settings in "DEBUG->Exceptions..." with your coworker
The ScriptResource.axd actually does create runtime generated JavaScript.
Have you tried using the continue button? It might cause the follow up error to appear.

Microsoft Visual Studio 2012 cannot set breakpoint in c# file

I have Microsoft Visual Studio Professional 2012 installed, version 11.0.60610.01 Update 3.
When debugging a c# (.cs) file Visual Studio gives me the following message when I try to set a breakpoint:
"A breakpoint could not be inserted at this location".
I get this message even when trying to set it on a line within a method. But in a .vb file for a Visual Basic app, I can set a breakpoint.
I am wondering if anyone has any suggestions to resolve this or if I need to reinstall visual studio.
Thanks
Maybe I'm too late for this question but here it goes anyway,
BUILD > Clean Solution
BUILD > Build Solution
I have encountered a similar issue and I resolved it by exiting Visual Studio and deleting the .suo file from my solution folder.
This file is recreated when you open the project again and it is not harmful to delete it.
The .suo is used for storing the layout of your solutions, the breakpoints you've set, the tabs you had open, etcetera.
I am not sure why this worked but my logic was that Visual Studio thought I was trying to place a breakpoint in a location different to where I was actually placing it.
I was finally able to find a solution for this. I had to do a repair on my Visual Studio 2012 instance through the control panel -> Programs and Features, right clicking on Microsoft Visual Studio Professional 2012, and selecting change. In the Visual Studio window I then selected repair.
As part of the repair process, I also had to download web deploy located here: http://www.microsoft.com/en-us/download/details.aspx?id=4148 and point the visual studio repair process to the .msi file when it said it couldn't find the web deploy package and could not download it from the internet.
I also had to implement the fix indicated in the following stackoverflow question: Plain C# Editor in Visual Studio 2012 (No intellisense, no indentation, no code highlighting)
Now I am able to debug applications as expected.
Well, sheesh...for people as dumb as me, here's one more thing to consider:
You can put breakpoints on the curly braces at the start or close of a method, and you can put breakpoints on any line that is doing something (e.g. assigning a value or calling a method). However, you can't put a breakpoint on a line that is only declaring a variable or otherwise "doing nothing."
E.g. I had a method:
public IEnumerable<SomeObject> GetList()
{
int distance;
var otherVar = SomeValue;
}
I was trying to put the breakpoint on the first line with int distance;, which is something that works fine in other IDEs, but that doesn't work in VS. I had to go up to the brace or down to the next line with the assignment in order to get the breakpoint to set.
5 minutes of my life wasted, that I'll never get back, trying to debug a non-issue ;-p
VS 2017
I had this, I was missing an ; inside a for loop
If there is no instructions to execute on a line, VS refuses to set a breakpoint an offers no reason. EG
string str; //Cannot set breakpoint
string str = ""; //Can set breakpoint

C# gives anonymous error

while i am calling a method which is declared in another project. calling project and called project are presented in same solution.
I am getting a error window that says:
Window Tile : Test.vshost.exe
Test.vshost.exe has encountered a problem and needs to close. we are sorry for the inconvenience.
(I will make disappear the error window by clicking "Dont Send" button)
what is the reason for such this error.
Note: I have included the refereed project in reference.
I used call a methode as follows
ReconcileDBService.InsertRecordInT_AUDIT_Incident(keyCase);
Above methode declared as follows
public static void InsertRecordInT_AUDIT_Incident(string keyCase)
We can't really help you based on the data you provide...
Is your program using Win32 or COM interop ?
Try to add breakpoints and debug messages to pinpoint the problem. Once you know the exact location where the problem occurs, it will be easier to understand it.
Turn off the "Visual Studio hosting process" and start your session again. Maybe you get some better or more information about an exception that is raised by your programm.
You can do this under the Project-Properties-->Debug-->Enable the Visual Studio hosting process (uncheck)
Also you could check the build order of your projects. It may be that the "Visual Studio hosting process" uses a diffrent assembly version than your programm expects.
Press Ctrl-Alt-E (Debug->Exceptions) and in the 'Break when an exception is:' selection, select everything.
Start a debug session with the application.
The debugger should now stop at the crash and you should be able to look what you are sending to the other project, and maybe solve your problem.

Categories

Resources