A Cool Trick for Passing Data to a Silverlight Application

As you probably already know, there are quite a few ways to pass data to a Silverlight application.

  • Use InitParams in the Silverlight plugin declaration
  • Read data off the querystring
  • Use WebClient to request a value
  • Interact with the HTML DOM to pull a value from the hosting page

But what do you do if you don’t have control over the hosting page?  Really, your only option is to make a call to a web server using WebClient.  But even that can have cross-domain policy implications.  Besides, that’s a lot of work to be doing just to simply supply a small amount of data to your application.

There is one other option however.

Application.Current.Host.Source will provide you with the URI of the XAP package that the host will render.  So what this means is that when you provide the URI for your XAP package, you can append a querystring to the end, like so:

<param name="source" value="ClientBin/MyApp.xap?MyKey=Testing"/>

Now, you simply need to read (and parse) the querystring in your application.  Since Silverlight does not automatically parse the querystring for us, we have to do the dirty work.  Yes, there are many ways you can do achieve this.  Below, you will find one technique:

private string GetXapPackageQueryStringValue(string key)
{
    if (string.IsNullOrEmpty(key))
        return string.Empty;

    key = key.ToLower();

    string xapPackage = Application.Current.Host.Source.OriginalString;
    int questionMarkIndex = xapPackage.IndexOf('?');
    if(questionMarkIndex == -1)
        return string.Empty;

    string queryString = xapPackage.Substring(questionMarkIndex, xapPackage.Length - questionMarkIndex); 
    queryString = queryString.Replace("?", string.Empty).ToLower();

    string[] keyValuePairs = queryString.Split(new[] { '&' });
    for (int i = 0; i < keyValuePairs.Length; i++)
    {
        string[] pair = keyValuePairs[i].Split(new[] { '=' });
        if (pair[0] == key)
        {
            return pair[1];
        }
    }

    return string.Empty;
}

Then, you can retrieve the querystring value like so:

string val = GetXapPackageQueryStringValue("MyKey");
// TODO: Use val in your code.

This can be useful in situations when you have the ability to specify the path to a XAP package, but don’t have control over any other facets.  One example of this would be CodePlex.  On CodePlex, you have the ability to embed Silverlight applications in your Wiki.  You do this with the following syntax:

{silverlight:url=http://mydomain.com/MyApp.xap?MyKey=testing,height=300,width=500}

Now, you can build one application and reference it multiple times while varying the querystring for each declaration.  This is really helpful when you want to write one Silverlight application that behaves differently when you change the querystring.  For example, on Silverlight Contrib, we have a tutorial application that will display one control for each different parameter that is passed in. 


Feedback

# re: A Cool Trick for Passing Data to a Silverlight Application

Gravatar hi, your solution works great but it doesn't work if we're using url rewriting, since it retrieves the "friendly url" instead of the url with all querystring values. is there a good way to fix it?

pls send me an e-mail if you figured out anything :-) thx 6/9/2009 5:44 AM | Dario

Comments have been closed on this topic.