Geeks With Blogs

News


Timmy Kokke's Blog

↑ Grab this Headline Animator

Timmy Kokke at Blogged
Timmy Kokke …just sorting my bubbles…

Ofter there are situations where it’s needed to pass parameters to Silverlight. For example a webshop where you would like to display a product, or a Twitter application where you would like to show information about a certain user.

The Silververlight control has an string property InitParameters. When passing any string to this property it can be read by the Silverlight application. Where the query parameters property in Asp is a string, in Silverlight this is changed into an IDictionary<string, string>. It is passed to the Startup event of the Silverlight application. Let me explain with an example.

 

First create a new Silverlight Website. This will create two projects. A Silverlight project and an Asp.Net webproject to host the silverlight control.

Screenshot_NewSilverlightProject

Delete the .html and the .aspx file with the similar names (we don’t need those), But keep the default.aspx file. Right-click this file and make it the start-up page.

At the top of default.aspx file add:

<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls" 
TagPrefix="asp" %>

Now add the Asp scriptmanager and the Silverlight control to the body of the document:

    <form id="form1" runat="server" style="height:100%;">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div  style="height:100%;">
            
            <asp:Silverlight 
                ID="Xaml1" runat="server" 
                Source="~/ClientBin/SilverlightApplication4.xap" 
                MinimumVersion="2.0.31005" 
                Width="100%" 
                Height="100%" />             
        </div>
    </form>

 In the codebehind file, Default.aspx.cs, add the change the Page_Load event handler to:

protected void Page_Load(object sender, EventArgs e)
    {
        Xaml1.InitParameters = this.ClientQueryString;
    }

This will pass the query parameters to the InitParameters property of the silverlight control.

Now let us take a look at the Silverlight part of the equation. When a Silverlight application is started the StartUp event is fired. The StartupEventArgs class that is passed to the event handler has a property InitParams. This property is an IDictionary<string, string> and can be easily iterated in a foreach loop. To demonstrate this, change the Application_Startup event handler in the App.xaml.cs file.

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new Page();

            foreach (var item in e.InitParams)
            {
                Console.WriteLine(item.Key + " => " + item.Value);
            }            
        }

Now when you hit F5 and start the website, nothing happens. But when you add “?username=test&string=hello%20world” to the url, this will be send as a parameter to your silverlight control and shown in the output window in Visual Studio.

Posted on Tuesday, December 16, 2008 10:37 AM Silverlight | Back to top


Comments on this post: Sending queryparameters to Silverlight

# re: Sending queryparameters to Silverlight
Requesting Gravatar...
Very helpful your small example to pass parameters to Silverlight.
Left by Silverlight travel on Dec 17, 2008 7:40 AM

# re: Sending queryparameters to Silverlight
Requesting Gravatar...
Why would you manually pass the querystring parameters in? There are several reasons why would not want to do this way.

Just use the native HtmlDocument object inside Silverlight to get access to a dictionary of all the querystring parameters:

IDictionary<string, string> questringParameters = HtmlPage.Document.QueryString;
int count = questringParameters.Count;
Left by Bart Czernicki on Dec 17, 2008 8:14 AM

# re: Sending queryparameters to Silverlight
Requesting Gravatar...
We found it very helpful in the startup handler to pass the query strings to a helper object that parses them into typed parameters, performs validations rules, and otherwise preps them to be useful in the application.

This also allows us to combine unique parameters introduced by the page with the query parameters available on HtmlPage.Document.QueryString for maximal flexibility.
Left by Eric Willeke on Dec 17, 2008 3:45 PM

Your comment:
 (will show your gravatar)


Copyright © Timmy Kokke | Powered by: GeeksWithBlogs.net