Error while loading designer in wpf form

3.6k views Asked by At

My application is working fine, all of a sudden while loading a designer in wpf form, im getting errors.

Object reference not set to instance of an object.

As if it is in a loop. After pressing the enter button for sometime. Im getting this error.

Microsoft Visual Studio XAML UI Designer has stopped working.

And after this, im getting this.

System.Runtime.Remoting.RemotingException [9980] Designer process terminated unexpectedly!

The number in square brackets is changing evrytime.

Im using Visual Studio 2012. Im not yet running the application, Im just switching to designer mode from code behind. If I compile and run the application, it runs fine. Kindly help.

Edit:

Here are the three errors in single image. (I cant post morethan one link)

xamlerrors

2

There are 2 answers

4
B.K. On BEST ANSWER

There's an error either in the code-behind or DataContext (if you're using a ViewModel). After you fix that error, click on Click here to reload the designer. What's happening is that the designer is trying to load everything up during design time and since there's an error, it's unable to, as if the program was actually running. That null reference exception can be a bit misleading in terms of trying to figure out the cause, because it'll be thrown if you have an error in your code-behind or ViewModel.

Things to check:

  1. Is your View referencing the correct ViewModel?
  2. Is your code-behind portion of the View matching the View name? Some people copy their Views, but forget to change the Class name in the code-behind.
  3. Do you have any errors in your ViewModel?
  4. What about models? If they're loaded during design time and contain errors, this could cause the above exception.
2
Martin On

I noticed that those kind of errors are almost everytime caused by

  • an error in the parameterless constructor of the view
  • an error in the constructor of the view model which is attached to the view

Keep in mind that when the designer loads, it calls the parameterless constructor of the view (there has to be one for the designer!). If there are "complex things" done there which can only performed correctly at runtime, there will likely be errors at design time.

The same is true for the constructor of the view model which is called from the view.


Check whether design mode is active

For example you should not load data from a repository in a constructor. If you do it in the constructor, at least check whether the design mode is active or not like explained in this answer here about the GetIsInDesignMode attached property and only do the complex logic in the constructor if the design mode is not active.


Debugging the problem

You also have the possiblity to debug the problem. In order to do this, you have to open a second instance of Visual Studio and debug the designer process of your original Visual Studio instance from there.

The process is described in detail here: Debugging an Exception (only) occurring in design view