SIGN UP MEMBER LOGIN:    
ARTICLE

Passing startup parameters in a Silverlight 2 application

Posted by Nipun Tomar Articles | Silverlight with C# March 22, 2009
This article describes how to pass startup parameters to a Silverlight application.
Reader Level:
Download Files:
 

ASP.Net Silverlight control allows passing startup parameters through InitParamaters property. You can specify the parameters as commas separated key-value pair string as follows in aspx page

<asp:Silverlight ID="Xaml1" runat="server" InitParameters="key1=value1,key2=value2" Source="~/ClientBin/InitParamSilverlight.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%" />

OR

Declare the initParams param on the Silverlight plug-in and pass in a comma-delimited set of key-value pair tokens as follows in html

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">

                  <param name="source" value="ClientBin/InitParamSilverlight.xap"/>

                  <param name="onerror" value="onSilverlightError" />

                  <param name="background" value="white" />

                  <param name="minRuntimeVersion" value="2.0.31005.0" />

                  <param name="autoUpgrade" value="true" />

                  <param name="initParams" value="key1=value1,key2=value2" />

                  <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">

                  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>

                  </a>

            </object>

Extract Parameters

Extract the initialization parameters using StartupEventArgs.InitParams property in the Application's Startup event handler.

private void Application_Startup(object sender, StartupEventArgs e)

        {

            if (e.InitParams != null)

            {

                foreach (var item in e.InitParams)

                {

                    this.Resources.Add(item.Key, item.Value);

                }

            }

            this.RootVisual = new Page();

        }

Access and read the values on page.xaml.cs

private void UserControl_Loaded(object sender, RoutedEventArgs e)

        {

            if (App.Current.Resources.Contains("key1"))

                TextBlock1.Text += " key1="+App.Current.Resources["key1"].ToString();

            if (App.Current.Resources.Contains("key2"))

                TextBlock1.Text += " key2="+App.Current.Resources["key2"].ToString();

        }

Things work fine if the initParams string is restricted to alphanumeric values. But if you want to use any special character then replace the special character with its URL encoded value and then in application use Uri.UnescapeDataString to decode that value as below

Aspx page (%2B is encoding of +)

<asp:Silverlight ID="Xaml1" runat="server" InitParameters="key1=value1,key2=value21 %2Bvalue22" Source="~/ClientBin/InitParamSilverlight.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%" />

App.xaml.cs


private void Application_Startup(object sender, StartupEventArgs e)

        {

            if (e.InitParams != null)

            {

                foreach (var item in e.InitParams)

                {

                    this.Resources.Add(item.Key, Uri.UnescapeDataString(item.Value));

                }

            }

            this.RootVisual = new Page();

        }

The resulting output is

Untitled.jpg

 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor