Geeks With Blogs

News




What I do:

Identity Mine

MVVM Light

GalaSoft


What I am:

Microsoft Most Valuable Professional, Client Application Development

Microsoft Certified Technology Specialist, Windows Presentation Foundation

WPF disciples


Social:


View my profile on LinkedIn

XING
Creative Commons License
Diary of a Code Trotter by Laurent Bugnion is licensed under a Creative Commons Attribution 3.0 Unported License

All source code on this blog is licensed under the MIT license.

Copyright (c) 2006 - 2011 GalaSoft Laurent Bugnion

Laurent Bugnion (GalaSoft) Diary of a Code Trotter
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 } } }
Posted on Wednesday, March 14, 2007 8:49 AM | Back to top


Comments on this post: WPF trick: Debugging the XamlParseException

# re: WPF trick: Debugging the XamlParseException
Requesting 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.
Left by Daniel Puzey on Mar 14, 2007 11:33 AM

# re: WPF trick: Debugging the XamlParseException
Requesting Gravatar...
Very cool, thanks for the feedback, Daniel!
Left by Laurent on Mar 14, 2007 11:41 AM

# re: WPF trick: Debugging the XamlParseException
Requesting 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.
Left by Patrick Sears on Aug 29, 2007 3:58 PM

# re: WPF trick: Debugging the XamlParseException
Requesting 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
Left by Laurent on Aug 30, 2007 7:44 AM

# re: WPF trick: Debugging the XamlParseException
Requesting Gravatar...
Actually, I get Patrick's solution on VS2005 v8.0.5. Laurent, can you not update fully and get it?
Left by Kurt on Sep 19, 2007 7:16 AM

# re: WPF trick: Debugging the XamlParseException
Requesting 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
Left by Laurent on Sep 20, 2007 7:50 AM

# re: WPF trick: Debugging the XamlParseException
Requesting Gravatar...
Tools->Options->Debugging->General->Check Enable Exception Assistant
Left by Rick on Sep 28, 2007 7:38 AM

# re: WPF trick: Debugging the XamlParseException
Requesting Gravatar...
Every day I learn new things... Rick, you're my new thing today :-) Thanks
Left by Laurent on Sep 28, 2007 7:55 AM

# re: WPF trick: Debugging the XamlParseException
Requesting 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
Left by Narendra on Sep 29, 2007 1:27 AM

# re: WPF trick: Debugging the XamlParseException
Requesting 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
Left by Bill on Oct 20, 2007 10:26 AM

Comments have been closed on this topic.
Copyright © Laurent Bugnion | Powered by: GeeksWithBlogs.net | Join free