views:

592

answers:

4

I run into the following issue semi-regularly: I make changes to XAML or some resources used by it and when I go to load up the Silverlight project in debug mode it only gets as far as the spinning Silverlight-loading animation.

I've tried attaching the VS08 debugger to the process but it doesn't do anything at this point (works fine once I'm in the Silverlight but not before.)

From previous experience I've noticed this happens when there're problems with the XAML or the resources in it but my only solution so far has been to dissect the code line-by-line until I spot the problem.

Is there an easy way to debug/diagnose these situations?

UPDATE

I found this question with some help, but it still doesn't provide a good way to debug these types of issues.

+1  A: 

This may not apply, but one frequent source of XAML errors is due to uncaught exceptions within converters that you're using as resources. People often forget to use a try-catch block in their converters, and when something blows up in there you end up having to dissect your code line by line.

And, take this with a grain of salt, but depending upon the situation, you may be able to copy-and-paste some of your XAML into a WPF project and get better error messages. I've never relied on this tactic myself, but I recently heard about it from an experienced WPF/SL developer who's far smarter than me, so it might be worth a shot. :-)

Ash
+3  A: 

This has been a real pain to debug but I finally found the problem hidden deep in the constructor to one of our custom controls (which was looking for a resource that wasn't there.) The real problem isn't fixing the issue but finding it.

I found that IE responds to exceptions in passed from Silverlight to the DOM but you don't get that same sort of feedback in the Chrome browser (which I use.) A solution that actually helps a great deal (even moreso than the IE tip) is to modify the ReportErrorToDOM() method in App.xaml.cs to the following:

private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
    string errorMsg = String.Empty;
    try
    {
        errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
        errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

        System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
    }
    catch (Exception)
    {
#if DEBUG
        MessageBox.Show(errorMsg);
#endif
    }
}

This gives you the position in the XAML where the issue is starting. It's not an ideal debugger, but it does help.

Nick Gotch
+1 Wow! A great find, have to try it out.
texmex5
A: 

Per microsoft: The end user at this time cannot debug the XAML Parser, only MS can do this. The new version of Silverlight 4 XAML Parser has been completely re-written using managed code and hopfully they will give us a better way to debug it. I know this as I just had an issue and opened a support case and was informed this by a Silverlight Dev.

The clkosest I could ever come to seeing what was going on with the parser was using Silverlight Spy:

http://firstfloorsoftware.com/silverlightspy/download-silverlight-spy/

Great tool.

James Campbell
A: 

This is not the ultimate answer but can often help

In Visual Studio :

  • Click Debug > Exceptions
  • Click 'Find' and search for XAML
  • Click the 'Thrown' button next to System.Windows.Markup.XamlParseException

Start your project in debug mode. Any Xaml exceptions will be shown immediately. Check the inner exception sometimes for further information.

I wasted soooo much time before I finally figured this one out!

Simon_Weaver