Ziff Davis EnterpriseDevLife
Advertisement

Thursday, September 13, 2007 11:59 AM/EST

Debugging Dynamic XAML in Silverlight

In the work I have been doing in Silverlight 1.0, I have had to write some XAML dynamically and then use CreatefromXAML to create the actual objects.

Here's a slice of some javascript code that builds up a XAML string

If you have invalid XAML in there, it's pretty tough to track down.

A typical error message is

AG_E_RUNTIME_METHOD: CreatefromXaml

In other words: "something is wrong with your xaml, but I don't know what. Sorry."

I have a few approaches to solving the problem.

The first is to grab the dynamically generated XAML. In this case I'm using an InkPresenter StrokeCollection where I have intentionally changed a DrawingAttributes color value ot a double rather than an RGB string like "#FF00000".

You can see the string in debug by hovering over the string variable that contains it:

but it's pretty hard to find the problem looking through that single line, which is very long.

You can use the debugger visualizer to see it as xml. (Click on the little arrow next to the magnifying glass.) Strings have 3 visualizers. Here is that string in the XML Visualizer:

This is a lot more helpful and sometimes I can spot the problem right away.

But other times, it's still not enough. Yesterday I was updating a 1.0 app that was a very cool MIX07 demo on annotation which no longer works in the released version of Silverlight 1.0. In there are some dynamically generated canvases with photos. The routine iterates through the available photos and creates a new canvas for each one along with some Storyboard animations.

But I was getting the AG_E_RUNTIME error method. In this case, not on CreateFromXaml, but when I was adding the canvas to the parent canvas.

AG_E_RUNTIME_METHOD: Add

I reviewed (yet again) the list of breaking changes from Beta 1 to RTM and made sure I had applied the fixes to the code that builds the XAML string. Still no luck.

So here's how I went about solving it.

First, I grabbed the xaml string from debug. You can do this by right clicking on the string debug view and choosing Copy Value.

I actually saved this in a file and triple checked by opening that up in XML Notepad 2007 (a free tool from Microsoft, basic but super handy), but still didn't see the problem.

I then pasted the dynamically generated XAML into the XAML file in my solution - into the spot that it was getting added dynamically. i put a breakpoint on the first line of code in root_Loaded. By the time I get to that point, I should already have gotten through the XAML file.I tried to run the solution to see if I could view the page, which I couldn't.

Now I was getting an error message ontop of my browser screen that said:

AG_E_INIT_ROOTVISUAL

That was clear enough to me that it was having hard time intializing the visual elements somewhere in the XAML.

So i commented and uncommented sections of the new XAML until the page finally displayed.

I was able to find the one line of XAML that was causing the problem. And it was because of a breaking change. The Animation was using a trick with a Rectangle that did not exist. But with the release, you cannot use this type of trick any more.

I returned to the string builder and added the extra rectangle using the name that the animation was looking for. I had come across a similar error before which is why I was able to finally spot it here once I knew which line of code was the problem. Had I been thinking of that change, I should have spotted it right in the string builder. But this is how things work, right?

TrackBack

TrackBack

http://blogs.devsource.com/cgi-bin/mte/mt-tb.cgi/11714

Post a Comment

 
 

Advertisement

Syndication

Subscribe: