Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / WPF
Tip/Trick

How to Debug the Design Time Errors in WPF XAML File?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
20 Oct 2015CPOL3 min read 44.1K   109   10   14
How to debug the design time errors in WPF XAML file?

Introduction

While developing WPF applications, design view plays an important role not only placing the controls but also we can see the run time view at design time. How does it look like? It becomes frustrating when we see some design time errors and we cannot put a break point in XAML file to diagnose the error, moreover due to this single error sometimes, whole designer fails rendering other controls. So this article enables us to debug the design view of XAML documents in WPF.

Background

It is a pre-requisite that one should be familiar with basic WPF and most importantly one should know how to set design time data context. You can refer to other articles on CodeProject to know how to set a design time data context like this one.

Problem

While designing WPF applications, we frequently see the following types of error in our design view (See pic). Since XAML code does not allow us to insert a break point and debug the stuff, I will share a small trick to trap this error. (References are already there on the internet, but still developers are not so habituated to using it. The reason is that most of them don't know it.)

Steps To Debug the Design Time Errors

  1. First of all, close all opened XAML documents in Visual Studio.
  2. Open the new instance of the same application in Visual Studio (say app2).
  3. Again, close all opened XAML documents. (To be on the safe side, you may close all documents in app2).
  4. Now open Task Manager just to verify whether XDesProc.exe must not be running. (Basically XDesProc.exe is responsible for debugging XAML files, so if any XAML documents are opened, then it launches automatically). So, if you find that XDesProc.exe is running (in Processes Tab), just kill it (right click on process and click on End Process Tree option).
  5. Now switch to app1 (original instance of Visual Studio in which you want break point to be hit). Now, open the file containing the design data view model (MainWindowViewModelDesignData.cs) and place a break point in the first line of its constructor.
  6. Switch to app2 again and open the View (i.e. MainWindow.xaml in which design time error is raising). It will launch the XDesProc.exe in Task Manager.
  7. Switch to app1 again, and go to Debug -> Attach to Processes... context menu item in Menu bar of Visual Studio.
  8. Search the XDesProc.exe and click on attach button.
  9. Switch to app2 and close and reopen the same XAML document (MainWindow.xaml). Once you do it, break point will get hit!

  10. Here, you will find that you have not instantiated the PersonList property which is causing NullReference Exception in WPF Designer. After fixing it, you will see that now the designer is showing the data as well.

Points of Interest

For Visual Studio 2010 users, you will find devenv.exe in place of XDesProc.exe. Also, you can tweak with the exception settings if your break point does not becomes active (i.e., Go to Debug -> Exceptions settings -> Common Language RunTime Exceptions).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUpdate for later versions of Visual Studio Pin
awroggeband28-Jul-22 7:55
awroggeband28-Jul-22 7:55 
GeneralMy vote of 5 Pin
kaardinator23-Jan-18 0:51
kaardinator23-Jan-18 0:51 
QuestionBrilliant! Pin
kdmote14-Dec-17 4:35
kdmote14-Dec-17 4:35 
PraiseThanks Pin
Bernhard Hiller25-Jul-17 23:59
Bernhard Hiller25-Jul-17 23:59 
Question5 from me Pin
Nick Polyak8-May-17 4:50
mvaNick Polyak8-May-17 4:50 
AnswerRe: 5 from me Pin
Manish Dubeyy16-May-17 21:30
Manish Dubeyy16-May-17 21:30 
GeneralMy vote of 5 Pin
aureole827-Oct-16 5:54
aureole827-Oct-16 5:54 
QuestionExactly what I needed Pin
a0110110118-Jul-16 5:18
a0110110118-Jul-16 5:18 
AnswerRe: Exactly what I needed Pin
Manish Dubeyy11-Jul-16 21:22
Manish Dubeyy11-Jul-16 21:22 
QuestionXaml Desgin time Errors Pin
GerVenson21-Oct-15 1:37
professionalGerVenson21-Oct-15 1:37 
A lot of Desgintime errors are false positiv.
Its right that the Interpreter is not as good as it should be but with a bit of experience you can easly determinate what the real error is. For example 2 Xaml files. The First one depends on the 2end one because it defines a DataTemplate that is using the 2end one. (like the App.xaml defines a datatemplate in its Resources). When now the 2end one has an error, the first one will also report one. Simply because the Parser is trying to parse the App.xaml ( because its our root element ). The App.xaml is taking care of the 2end one ( our DataTemplate ). Now the 2end one is to be rendered in the step of the 1st one. If this fails, the Parser will show you first that the 2end document has an error and because it was about to render the App.xaml and this failed, the app.xaml will also be show you an error. 1 Real error and one FalsePositiv. The Trick to determinate is simple. Start with your lookup for the real error, always on the most lower document that exists and ignore all upper documents as long as there is still an error in one of the lower documents.

If you get this kind of "knowledge" about the xaml parser, you will be very effective in finding xaml errors. This is some sort of right behavior because its simple.

SuggestionTry-Catch Pin
Michael Röttges21-Oct-15 1:28
professionalMichael Röttges21-Oct-15 1:28 
Questionimages? Pin
Simon_Whale20-Oct-15 22:56
Simon_Whale20-Oct-15 22:56 
AnswerRe: images? Pin
DotNetSoldier21-Oct-15 3:38
professionalDotNetSoldier21-Oct-15 3:38 
QuestionGiven up Pin
RugbyLeague20-Oct-15 21:40
RugbyLeague20-Oct-15 21:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.