0

How do you handle when the editor (xaml / design) in visual studio throws an argumentnullexception, "value cannot be null" ?

I am reading some values from the database using entity framework and storing them in an observable collection so I can bind to them from xaml. It is logic to suppose that there might not be any values in the collection so the exception is correct, right? Even if I have values in the database, the exception is still thrown because the editor (when viewing in design mode) is not running the application to get the values, which is still normal.

When I run the application, everything works fine but the exception is very annoying.

I also get an error, using a try catch when reading from the database, but only in editor (xaml) not when running the application, that says "The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid. The stacktrace gets me into the entity data model itself. I am using mysql as a backend. This messagebox with the error is thrown everytime I type something (a character) in xaml !

My unit and integration tests all work fine.

Btw is this normal ?

EDIT: If I add the initialization of the viewmodel in the app resources (in app.xaml.cs but not in the app.xaml) it seems to work. I don't see the errors when I write the code in xaml or when I switch to design to see how it looks.

leppie
  • 115,091
  • 17
  • 196
  • 297
amb
  • 1,599
  • 2
  • 11
  • 18

2 Answers2

1

You can design some sample data for the design mode so that you can see the designer and write code in xaml without any designer exception. For this, you might need to use DesignerProperties class (e.g DesignerProperties.GetIsInDesignMode(this) etc). I have faced similar XAML designer crush and solved using this technique. I would recommend reading this guideline for further assistance.

Amit
  • 256
  • 1
  • 6
  • I think this will solve part of my problem (with the ArgumentNullException). Since my workaround worked, I think it will also solve the error from catch, even though I don't understand it. – amb Jul 06 '11 at 08:15
0

You can debug the designer :-)

For this make a breakpoint in your model (at the beginning)

now in the project options under "Debug" chooose "start with external program" ans select the devenv.exe

now when you run your project, a new visual studio instance will appear. When you open your window in the new visual studio instance, the break should be hit in the first instance of visual studio

Boas Enkler
  • 12,264
  • 16
  • 69
  • 143
  • Thanks but when I am in editor it stil bugs me with the argumentnullexception in design and in xaml I see the messagebox from the catch. Of course if I remove the messagebox I will see all the stacktrace in the argumentnullexception in the design. – amb Jul 06 '11 at 07:05