Saturday 28 November 2009

Silverlight: how to decode a uri and pass the arguments to your method

This post has moved to http://www.scottleckie.com/2009/11/silverlight-how-to-decode-a-uri-and-pass-the-arguments-to-your-method/
The problem is that you need to pass your parameters to your Silverlight class and hopefully deal with them. First and foremost, how do you recognise that you’ve been called? Well, via the OnNavigatedTo event… this event is fired for every Silverlight page.
OK, so we captured the OnNavigatedTo event. What next? Well, next we try to decode the uri that was passed to the SilverLight app. Let’s pretend for a moment that we were called via “http:<YourSource>?title=myTitle&search=special”
We now have two session variables set; title=myTitle and search=special
So, how do we decode these? The easiest way is to use the SilverLight libraries to query the NavigationContext object;
if (this.NavigationContext.QueryString.ContainsKey("title"))
                this.Title = this.NavigationContext.QueryString["title"];
if (this.NavigationContext.QueryString.ContainsKey("search"))
                searchType = this.NavigationContext.QueryString["search"];