Tuesday, June 10, 2008

Converting from Silverlight Beta 1 to Beta 2

As usual, the transition from one beta to the next brings breaking changes... just going to list the ones I had here, and how I got around them:

SetValue Only Accepts the Correct Types (No Conversions)
Before I was passing in Strings for colors when manually creating my xaml elements, or I was using strings for some other properties where I shouldn't have been.
For example:
Beta1: myRect.setValue(Rectangle.FillProperty, "red");
Beta 1: myRect.setValue(Rectangle.StrokeThicknessProperty, "2");

This was nice because all the conversion to a SolidColorBrush was done for me... but alas they caught on to my laziness. So now I must do something like this:
Beta 2: myRect.setValue(Rectangle.FillProperty, new SolidColorBrush(Colors.Red);
Beta 2: myRect.setValue(Rectangle.StrokeThicknessProperty, 2.0);

I imagine I will also run into some issues when I need to convert from a HEX color, but I'll cross that bridge when I get there.


WebClient and HttpWebRequest Changes
This one really eluded me. This manifested in my code in the following way: I was using HttpWebRequest to get a xaml string directly from a file on my server, and then I was loading that xaml via xamlReader.load... BUT... I was now getting a Invalid cross-thread access" [System.UnauthorizedAccessException] exception.

Loading the EXACT same xaml from the page_loaded function worked fine... what the heck? From the advice of my project manager, I checked the Thread.CurrentThread.ManagedThreadId [System.Threading] of both the page_loaded and my httpWebRequest callback function and BAM... they are now in different threads.

Turns out there were a few things I was unaware of with Beta 2:
  • HttpWebRequest delegates were changed to return on a background thread.

  • HttpWebRequest is callable on a background thread

Also, what I wasn't aware of was this [which given the HttpWebRequest changes was brought to my attention]:
Solution?
Following the example shown on the Breaking Changes documentation I switched to using WebClient, which is now a much more robust class in beta 2.

My project manager also suggested I check out creating my own BackgroundWorker threads which would do any consuming processing I may need in the future, and then do a callback to my main thread when completed with the processed data that the main thread could use to create the desired XAML. I'm guessing BackgroundWorker will be my next blog post XD


More to follow I imagine...

No comments: