Debugging XAML Errors at Runtime
Reposting info that I posted on a forum thread. I’ve reordered and rewritten the content a bit.
When I have a mistake like this in my XAML:
Sometimes XAML intellisense will help you catch this kind of error…but not always. The XAML markup compiler doesn’t currently catch errors like this. So you end up with this "Greeen" compiled into the assembly you are running (inside of the BAML binary stream.)
Debugging with VS 2005 at runtime, you will see errors like this (bad error message and pointing to the wrong location):
Cannot create instance of ‘Window1’ defined in assembly ‘UsingWpfUtilities, Version=1.0.2593.18928, Culture=neutral, PublicKeyToken=null’. Exception has been thrown by the target of an invocation. Error in markup file ‘UsingWpfUtilities;component/Window1.xaml’ Line 1 Position 9.
We are working to improve this experience in VS "Orcas", but the best way to deal with this under VS 2005 is to:
ComboBox: Common Lanaguage Runtime Exceptions
Check the "Thrown" box for that newly added Exception (S.W.M.XPE)
Debugging the app (F5) will now catch the XamlParseException and give you the following message:
Cannot convert the value in attribute ‘Background’ to object of type ‘System.Windows.Media.Brush’. Token is not valid. Error at object ‘System.Windows.Controls.Grid’ in markup file ‘UsingWpfUtilities;component/window1.xaml’ Line 10 Position 6.
In builds of VS "Orcas" that aren’t too far away, we’ll do a few things to make this better:
1) The designer will get better, so that you’ll need to spend less time typing XAML by hand.
2) The XAML editor will no longer be based on XSD, so that we’ll be able to greatly improve the intellisense experience…red squiggles will really mean there is a problem!
3) At runtime, we’ll try to relate the runtime exception back directly to the right place in the XAML.