Explanation for a weird Visual Studio 2008 Silverlight 2 issue
Posted by andrewmyhre on January 22, 2009
I ran into a weird and annoying Silverlight/Visual Studio quirk today which cost me a little bit of time to work out the cause of, so here’s my story in case it helps anyone else out.
If you’re building a Silverlight Page/UserControl and you run it and instead of seeing your lovely application appear you see something like this:
Sys.InvalidOperationException: Invalid XAML for control 'Xaml1'. [
And you notice in the XAML where you have included your control some blue lines:
And when you place your mouse cursor over the lines you see something incomprehensible, like this:
I did today. After trying essentially random and arbitrary things to solve the issue (deleting and recreating the control, renaming the control, removing sections of XAML and code from the control) I finally narrowed it down to this:
In my constructor I was causing an null reference error, but it was being reported as a XAML error! How bloody confusing. And the reason this didn’t twig immediately is that I didn’t realise the XAML editor actually creates instances of the control tree and invokes the constructors of the controls in the process. Never occurred to me before, but makes total sense.
Anyway, so I fixed my code:
Rebuilt the solution and checked the containing control’s XAML:
No more blue lines – nice. Finally I ran my test page and lo and behold it worked!

![51] 031](http://andrewmyhre.files.wordpress.com/2009/01/031.jpg?w=300&h=83)


