WPF trick: Debugging the XamlParseException

Update: See more information about XAML debugging here.

Here's a trick: When you work a lot with XAML, you'll be confronted to the dreaded XamlParseException sooner or later. This exception is thrown whenever something goes wrong while the XAML code is parsed. However, this exception is not very helpful. Typically, you get a message box telling you that:
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Cannot create instance of 'Window1' defined in assembly 'GalaSoftLb.Wpf.MyApplication, Version=1.0.2629.15160, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'Window1.xaml' Line 1 Position 9.
Right... so what exactly went wrong? Well, it's easy to find out: In the code-behind, the XAML code is parsed in the method InitializeComponent which is automatically generated. This method is called in the Window object's constructor. So to have more details about the exception, put the call to InitializeComponent in a try/catch block. This way, you have access to the useless XamlParseException, but also to its InnerExceptions and to the StackTrace.
public partial class Window1 : System.Windows.Window { public Window1() { try { InitializeComponent(); } catch ( Exception ex ) { // Log error (including InnerExceptions!) // Handle exception } } }
Print | posted on Wednesday, March 14, 2007 8:49 AM

Feedback

# re: WPF trick: Debugging the XamlParseException

left by Daniel Puzey at 3/14/2007 11:33 AM Gravatar
There's another trick to this. Often, by default, you'll get an error reported at Line 1 of the xaml, which is an obvious lie. You can catch the original exception, though:

- Open the "Exceptions" window (Debug/Exceptions) in Visual Studio.
- Click "add"
- Add "System.Windows.Markup.XamlParseException"
- Check the box to break on throw for this exception.
- Hit F5!

You'll find that the XamlParseException you catch is much more descriptive, and will give the correct position in the xaml file.

# re: WPF trick: Debugging the XamlParseException

left by Laurent at 3/14/2007 11:41 AM Gravatar
Very cool, thanks for the feedback, Daniel!

# re: WPF trick: Debugging the XamlParseException

left by Patrick Sears at 8/29/2007 3:58 PM Gravatar
I realize this is 5 months after your post -

Another way you can do this is when the IDE breaks into the debugger and shows you the dreaded "Cannot create instance of 'Window1' blah blah" exception, click View Detail, and drill down on the InnerException property until you get to the real exception.

That way, you don't have to add the XamlParseException to the caught exceptions, and you don't have to wrap InitializeComponent() in a try-catch.

This method is essentially the same as catching the XamlParseException (it's exactly the same exception, obviously), except without the extra work.

# re: WPF trick: Debugging the XamlParseException

left by Laurent at 8/30/2007 7:44 AM Gravatar
Hi Patrick,

Yeah you're a lucky guy, doing WPF with VS2008 :-) Back when I wrote this, VS2008 was not really that ready yet, and your solution doesn't apply to VS2005.

VS2008 makes programming and debugging easier, no doubts :-) Thanks for your comment.

Laurent

# re: WPF trick: Debugging the XamlParseException

left by Kurt at 9/19/2007 7:16 AM Gravatar
Actually, I get Patrick's solution on VS2005 v8.0.5. Laurent, can you not update fully and get it?

# re: WPF trick: Debugging the XamlParseException

left by Laurent at 9/20/2007 7:50 AM Gravatar
Hi Kurt,

One of my colleagues was also able to use the "VS2008-like" exception message box in VS2005. Unfortunately, he and I don't know why he gets it, and I don't. I also have VS2005 V8.05

Microsoft Visual Studio 2005, Version 8.0.50727.762 (SP.050727-7600)

What is the exact version number you use?

Greetings,
Laurent

# re: WPF trick: Debugging the XamlParseException

left by Rick at 9/28/2007 7:38 AM Gravatar
Tools->Options->Debugging->General->Check Enable Exception Assistant

# re: WPF trick: Debugging the XamlParseException

left by Laurent at 9/28/2007 7:55 AM Gravatar
Every day I learn new things... Rick, you're my new thing today :-) Thanks

# re: WPF trick: Debugging the XamlParseException

left by Narendra at 9/29/2007 1:27 AM Gravatar
Thanks for bailing me out of crunch situation. Actually i am developing an WPF application in crunch deadline situation..

Ur post helped to figure out problem in the code

# re: WPF trick: Debugging the XamlParseException

left by Bill at 10/20/2007 10:26 AM Gravatar
Hi,

I'm having this same problem - plus the generated method call to InitializeComponet() cannot be found with the error "Cannot resolve symbol InitializeComponent". Everything builds and when I create a new Windows project using the Extentions to VS2005 for WCF/WPF it runs just fine. Just can't resolve the InitializeComponent. I spent 12 hours today uninstalling everything from VS2005 to the runtimes for both .net 2.0 and 3.0 and still have the same problem. Anyone seen this or know what's up?

thanks!
Bill
Comments have been closed on this topic.