Wednesday 17 February 2010

Debugging design time issues (and how F1 can sometimes be useful)

I was going through one of my projects and fixing any blendability issues I encountered. But things went a bit pear shaped and I started getting design time errors. Damn, I thought, what could possibly have gone wrong?

  1. Did I miss something from my own example?
  2. Is my example wrong? (that would just be typical...)
  3. Is something else I did not take into account go wrong?

Thankfully, I managed to avoid embarrassment as it was the 3rd point that was the case. In the default constructor of my ViewModel (the one invoked during design time) I had a silly error. But that is not the point of this post. The point is how did I discover it.

So how do you fix design time issues?

This may be old news to many but I never knew how to do debug during design time. This is explained adequatly well but I did get a bit confused getting it to work.

First, lets assume the following project structure:

  • MyApp (Shell project)
  • MyApp.Infrastructure (Infrastructure project, includes the ViewModelBehavior class)
  • MyApp.Modules.Test (just a test module)

In MyApp.Modules.Test add the usual loader class and the View and ViewModel classes (TestView and TestViewModel) along with their interfaces (ITestView and ITestViewModel) and connect them using the above example. In the constructor of the ViewModel, throw any odd exception. Now your designer will throw a mystical error and will not render. Help!

The table below shows the step by step instructions of how to debug this. There are two columns, one per Visual Studio instance, and is in a timeline format.

Step Instance 1 (debugger instance) Instance 2 (debug triggering instance)
1Right click on MyApp.Modules.Test then Properties-->Debug. In the Start Action group, select Start external program and locate the Visual Studio executable (usually in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe).
2Right click on MyApp.Modules.Test and Set as Startup Project.
3Press F5.New instance of VS will be launched.
4Open your MyApp solution.
5Open the TestView view.
6The debugger will hit the breakpoint in the constructor of TestViewModel.

And at that stage you should be able to fix any problem(s) (e.g. comment out the exception ;-) ).

The moral of the story here is not to mock the F1/Help functionality. I cannot remember the last (first?) time I used it but for some strange reason I decided to click on the Help on the designer surface when I was having problems. Following the steps I managed to locate the above linked article in the help and discover this cool little trick.

Great stuff.

No comments:

Post a Comment