Passing parameters to Silverlight

Just found something potentially very useful – how to pass parameters to one of my Silverlight demos so it can start up in a different state, or with a different preset loaded, depending on how it is called. Turns out this is very simple, provided you know the library command to use, as many things are. For the record, this is the easiest way I know of (and is far simpler than using initParams for what I wanted to do).

Quote is from a post by Sergey Volk from

“It’s much easier to use managed API for this, it will parse query string for you using browser conventions (e.g. it will split param=value pairs separated by ‘&’ symbol, it will unescape url-encoded characters, replace ‘+’ with space, etc). If you have html page, say MyHtmlPage.html, which contains your SL app, you could give user a link like MyHtmlPage.html?param1=abc&param2=def&param3=ghi, then in managed code you could access these parameters like this:

string abc = System.Windows.Browser.HtmlPage.Document.QueryString[“param1”];
string def = System.Windows.Browser.HtmlPage.Document.QueryString[“param2”];
string ghi = System.Windows.Browser.HtmlPage.Document.QueryString[“param3″];”

One issue is that it’s not clear what happens if the parameter is not in the query string, you might end up trying to set a string to null which could cause some problems later. So perhaps use something like:

string abc = System.Windows.Browser.HtmlPage.Document.QueryString[“param1”] ?? String.Empty;

I must remember this – this opens up a lot of possibilities…

Later note: in some cases the machine throws an exception when it can’t find the right item in the list of parameters, so I’ve been enclosing the call to QueryString in a try-catch block.

This entry was posted in Uncategorized. Bookmark the permalink.