Silverlight: Passing InitParams to your Application from the Page

To pass initialization parameters (InitParams) to your Silverlight application at its startup you can simply add a new  ‘initParams’ element to the Silverlight Object markup within your page. The ‘initParams’ element contains for its value a hash of key value pairs you wish to pass into the application in the for “key=value”.
Example:

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="ClientBin/JoxStrap.Silverlight.Home.xap"/>
    <param name="onError" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="3.0.40624.0" />
    <param name="autoUpgrade" value="true" />
    <param name="initParams" value="baseUri=http://local.joxstrap.com" />
    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
      <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
    </a>
     </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>

After adding the ‘initParam’ element to the object markup you then read in these parameters in the Application_Startup method located in your App.xaml.cs file.

private void Application_Startup(object sender, StartupEventArgs e)
        {
            StartUpParameters startup = StartUpParameters.Instance;

            try
            {
                string uriString = e.InitParams["baseUri"];
                if (!string.IsNullOrEmpty(uriString))
                {
                    Uri baseUri = new Uri(uriString);
                    startup.BaseUri = baseUri;
                }
            }
            catch
            {
                startup.BaseUri = null;
            }

            RootVisual = new MainPage();
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s