Sunday, September 7, 2008

Silverlight hidden detail: can´t write to UI while doing asynchronous calls

Let's get some data from a RSS feed. No big deal, because there's a pretty example in the silverlight site quickstarts.

However, I wanted to do my concept prove in VB.Net, and take the data from a List in Sharepoint.

As usual, Sharepoint let's you create your list, then you can add some items, and because I don´t know the trick to get directly the RSS reference, I publish the list contents in a Content query web part, activating the feed.

Ah, of course, you must add a Clientpolicyaccess.xml file on tje root of the site to get access.

But when I translate the code for my new Silverlight app, and ran it: surprise!!!! Nothing happends. So I check the IDE output and there's a message like this:

A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.dll

So I added a variable to get the content from the feed, and a button to launch the textbox refresh. The content reading was fine; and when the button was clicked the content was assigned fine, so where's the deal?

Thanks to the post from Mark Monster, I discover the trick: use

Dispatcher.BeginInvoque(Adressof YourUIUpdateOps)

inside the thread; you need to create a method called YourUIUpdateOps to define the UI ops that you need.

And all this is needed because you can't perform UI ops in threads used for async communications (beware of WS).

24 hours to get the answer. Such is life.

No comments: