3

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

Revanth
  • 73
  • 1
  • 8
  • When it gives the first error, it should include a link that shows the stacktrace. That should point to whichever object hasn't been created yet. – mcalex Nov 13 '14 at 06:53
  • @mcalex,Im not running the application yet, Im just opening the designer, and it is throwing me plain message box with that message, I could show u the screen shot but i dont have enough reputation points. – Revanth Nov 13 '14 at 06:56
  • Hmm, OK. Xaml designer errors I get usually show a stacktrace link at design time (sometimes, even when everything compiles OK). Maybe you could post an image on one of the image sites around (snag.gy, for instance)? – mcalex Nov 13 '14 at 07:08
  • Related post :[Editing XAML leads Visual Studio's Designer to crash](https://stackoverflow.com/q/28581312/465053) – RBT May 05 '21 at 04:57

2 Answers2

2

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.
B.K.
  • 9,982
  • 10
  • 73
  • 105
  • Im using viewmodel and there are no errors anywhere in the application. I faced same error with another page before but it resolved by itself. Now this page is not. Thanks for reply, kindly advice. – Revanth Nov 13 '14 at 08:22
  • @Revanth Post your `XAML` and your `ViewModel`; otherwise, it's just guessing. I'm basically describing what it could be based on what I've encountered in my experience with WPF. – B.K. Nov 13 '14 at 08:25
  • Thank you BK, following your suggestion, I created a new form and copied lines one by one. The problem is with one of the static instance binding. But it didnt throw any error from beginning. now I will dig through what is the reason. Thank you once again for the idea. – Revanth Nov 13 '14 at 09:56
  • @Revanth No problem. Debugging design-time and binding-related exceptions can be a pain once in a while. – B.K. Nov 13 '14 at 10:22
1

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

Martin
  • 5,165
  • 1
  • 37
  • 50
  • Yes. As per your suggestion, just now added a parameter less constructor. Adding it cleared all messaged in Error List about static instance binding in a xaml. Will follow same thing for the rest of bindings too. Thanks. – Revanth Sep 10 '15 at 09:34
  • Glad I could help :-) If it worked for you, please vote up my answer. – Martin Sep 10 '15 at 09:40