Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

One of my favorite features in Expression Blend is the ability to attach a Visual Studio debugger to Blend. First let’s start by answering the question: why exactly do you want to do that?

Note: If you are familiar with the creation and usage of design time data, feel free to scroll down to the paragraph titled “When design time data fails”.

Creating design time data for your app

When a designer works on an app, he needs to see something to design. For “static” UI such as buttons, backgrounds, etc, the user interface elements are going to show up in Blend just fine. If however the data is fetched dynamically from a service (web, database, etc) or created dynamically, most probably Blend is going to show just an empty element.

The classical way to design at that stage is to run the application, navigate to the screen that is under construction (which can involve delays, need to log in, etc…), to measure what is on the screen (colors, margins, width and height, etc) using various tools, going back to Blend, editing the properties of the elements, running again, etc. Obviously this is not ideal.

The solution is to create design time data. For more information about the creation of design time data by mocking services, you can refer to two talks of mine “Deep dive MVVM” and “MVVM Applied From Silverlight to Windows Phone to Windows 8”. The source code for these talks is here and here.

Design time data in MVVM Light

One of the main reasons why I developed MVVM Light is to facilitate the creation of design time data. To illustrate this, let’s create a new MVVM Light application in Visual Studio.

  • Install MVVM Light from here: http://mvvmlight.codeplex.com (use the MSI in the Download section). After installing, make sure to read the Readme that opens up in your favorite browser, you will need one more step to install the Project Templates.
  • Start Visual Studio 2012.
  • Create a new MvvmLight (Win8) app.
New MVVM Light (Win8) app
  • Run the application. You will see a string showing “Welcome to MVVM Light”.
Welcome to MVVM Light
  • In the Solution explorer, right click on MainPage.xaml and select Open in Blend.
  • Now you should see “Welcome to MVVM Light [Design]”
Welcome to MVVM Light [design]

What happens here is that Expression Blend runs different code at design time than the application runs at runtime. To do this, we use design-time detection (as explained in a previous article) and use that information to initialize a different data service at design time. To understand this better, open the ViewModelLocator.cs file in the ViewModel folder and see how the DesignDataService is used at design time, while the DataService is used at runtime. In a real-life applicationm, DataService would be used to connect to a web service, for instance.

When design time data fails

Sometimes however, the creation of design time data fails. It can be very difficult to understand exactly what is happening. Expression Blend is not giving a lot of information about what happened. Thankfully, we can use a trick: Attaching a debugger to Expression Blend and debug the design time code.

In WPF and Silverlight (including Windows Phone 7), you could simply attach the debugger to Blend.exe (using the “Managed (v4.5, v4.0) code” option even for Silverlight!!)

In Windows 8 however, things are just a bit different. This is because the designer that renders the actual representation of the Windows 8 app runs in its own process. Let’s illustrate that:

  • Open the file DesignDataService in the Design folder.
  • Modify the GetData method to look like this:
public void GetData(Action<DataItem, Exception> callback)
{
    throw new Exception();

    // Use this to create design time data

    var item = new DataItem("Welcome to MVVM Light [design]");
    callback(item, null);
}
  • Go to Blend and build the application.
  • The build succeeds, but now the page is empty. The creation of the design time data failed, but we don’t get a warning message. We need to investigate what’s wrong.
  • Close MainPage.xaml
  • Go to Visual Studio and select the menu Debug, Attach to Process.
  • Update: Make sure that you select “Managed (v4.5, v4.0) code” in the “Attach to” field.
  • Find the process named XDesProc.exe. You should have at least two, one for the Visual Studio 2012 designer surface, and one for Expression Blend.

Unfortunately in this screen it is not obvious which is which. Let’s find out in the Task Manager.

  • Press Ctrl-Alt-Del and select Task Manager
  • Go to the Details tab and sort the processes by name.
  • Find the one that says “Blend for Microsoft Visual Studio 2012 XAML UI Designer” and write down the process ID.
Task Manager
  • Go back to the Attach to Process dialog in Visual Studio. sort the processes by ID and attach the debugger to the correct instance of XDesProc.exe.
  • Open the MainViewModel (in the ViewModel folder)
  • Place a breakpoint on the first line of the MainViewModel constructor.
  • Go to Blend and open the MainPage.xaml again.

At this point, the debugger breaks in Visual Studio and you can execute your code step by step. Simply step inside the dataservice call, and find the exception that you had placed there. Visual Studio gives you additional information which helps you to solve the issue.

Exception in DataService

More info and Conclusion

I want to thank the amazing people on the Expression Blend team for being very fast in guiding me in that matter and encouraging me to blog about it. More information about the XDesProc.exe process can be found here. I had to work on a Windows 8 app for a few days without design time data because of an Exception thrown somewhere in the code, and it was really painful. With the debugger, finding the issue was a simple matter of stepping into the code until it threw the exception.

 

Print | posted on Wednesday, September 5, 2012 11:53 PM

Feedback

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by Chris at 9/6/2012 9:01 AM Gravatar
Hey Laurent,

To get this to work, I had to switch the 'Attach To' part of the 'Attach To Process' dialog to be be 'Managed (v4.5, v4.0) code', removing the 'Automatic' part, as it wouldn't load the debug symbols otherwise...

Ta!
Chris

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by Laurent at 9/6/2012 9:38 AM Gravatar
Hi Chris, very good point, I will update the article. Cheers!

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by Graham at 10/21/2012 12:22 PM Gravatar
Chris, that is strange..... magnificent find though, it helped me as well, simply would not debug on 'Automatic: Managed (v4.5, v4.0) code' which should be the same thing :)

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by Rob de Beir at 11/24/2012 5:43 PM Gravatar
Thanks a lot, did not understand why the attach did not work anymore, but thanks to your explanation got it working again.

By the way if you suspect (or as a first step) that there is an error in the design data service, just make a small change in the ViewModelLocator (make the design service temporarily the real service) and run the app to see if there is an exception (much faster to do this than the attach method, but you have to remember to switch back to the real service :)

if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<IDataService, DesignDataService>();
}
else
{
// in case you want to test the DesignService
// SimpleIoc.Default.Register<IDataService, DesignDataService>();
SimpleIoc.Default.Register<IDataService, DataService>();

}

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by Laurent at 11/24/2012 6:15 PM Gravatar
That's a good tip, and in fact I do use that often too. Sometimes the design tune data service throws other exceptions when run in the context of Blend, but this first easy can really help getting most of the errors out of the way :)

Cheers
Laurent

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by Josef at 12/20/2012 8:42 PM Gravatar
Folks, this drives me crazy. I fighting blend since a week or so. I do not get the design data Displayed nor it stopps at break Points in debug. What can I do to find out whats wrong.

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by Alan McBee at 2/12/2013 2:04 AM Gravatar
Laurent, you're a life-saver!

Also, I found that if you hover your mouse cursor over the two XDesProc.exe processes listed in the Attach to Process dialog, Visual Studio 2012 will provide a tooltip after a second that shows the entire path to the process. The paths are different; the one for Blend clearly appears to be in a path with the word "Blend" in the path, whereas the Visual Studio process does not.

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by Laurent at 2/12/2013 7:55 AM Gravatar
Well I didn't know that :) Good find.

Cheers
Laurent

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by James at 5/2/2013 11:31 PM Gravatar
This is not working for me. I followed directions and suggestions to the tee. I use VS 2012 ultimate.

# re: Adventures in Windows 8: Understanding and debugging design time data in Expression Blend

left by James at 5/3/2013 12:35 AM Gravatar
Nevermind. I Guess I was doing something wrong with Unity where the viewModel constructor was never getting called. I got it working with the default IoC container from the toolkit.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: