Export (0) Print
Expand All

How to: Specify and Retrieve Custom Initialization Parameters


When you embed the Silverlight plug-in in a Web page, you can specify custom initialization parameters in the plug-in configuration. These parameters are name and value pairs that you can retrieve in a handler for the Application.Startup event or in a IApplicationService.StartService method implementation through the ApplicationInitParams property. You can also retrieve these parameters at any time through the SilverlightHost.InitParams property.

Custom initialization parameters enable your host Web page to influence your application initialization. For example, you can use custom initialization parameters with a Silverlight-based clock control to specify a digital or analog display.

The following code examples demonstrate how to use initialization parameters.

The first example demonstrates how to specify custom initialization parameters in the host Web page. Then, it shows how to retrieve the parameters in a Startup event handler through the StartupEventArgs.InitParams property.

This example also demonstrates how to retrieve URL parameters at startup through the HtmlDocument.QueryString property. Note that you cannot retrieve the URL parameters if HTML access is disabled, which it is by default with cross-domain hosting. For more information, see Security Settings in HTML Bridge.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
    <script type="text/javascript" src="Silverlight.js"></script>
        <object id="slPlugin1" width="300" height="50"
            type="application/x-silverlight-2" >
            <param name="source" value="ClientBin/SilverlightApplication.xap"/>
            <param name="initParams" 
            <!-- Installation HTML omitted. -->
        <div id="silverlightControlHost">
            <script type="text/javascript">
                    silverlightControlHost, "slPlugin2",
                    { width: "300", height: "50", background: "white" }, { },
                    "id=slPlugin2,embeddingTechnique=createObject" );

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

    // This assumes that Page.LayoutRoot exists and is a StackPanel.
    StackPanel layoutRoot = p.LayoutRoot; 

    // Display the custom initialization parameters.
    foreach (String key in e.InitParams.Keys)
        layoutRoot.Children.Add(new TextBlock() {
            Text = String.Format(
                "from InitParams: {0} = {1}", key, 

    // Display the URL parameters.
    foreach (String key in HtmlPage.Document.QueryString.Keys)
        layoutRoot.Children.Add(new TextBlock()
            Text = String.Format(
                "from QueryString: {0} = {1}", key, 

The next example demonstrates how to access initialization parameters in an out-of-browser application. This example shows code from a Startup event handler that uses isolated storage to save initialization parameters when an application is launched inside the browser. After the user installs it for out-of-browser use, the code retrieves the initialization parameters from isolated storage.

IDictionary<String, String> initParams;

// When running outside the browser, retrieve initParams from isolated storage.
if (Application.Current.IsRunningOutOfBrowser)
    using (IsolatedStorageFile file =
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(
        "initParams.txt", System.IO.FileMode.Open, file))
        // The serializer requires a reference to System.Runtime.Serialization.dll.
        DataContractSerializer serializer =
            new DataContractSerializer(typeof(Dictionary<String, String>));
        initParams = (Dictionary<String, String>)serializer.ReadObject(stream);
// Otherwise, save initParams to isolated storage.
    initParams = e.InitParams;

    using (IsolatedStorageFile file =
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(
        "initParams.txt", System.IO.FileMode.Create, file))
        DataContractSerializer serializer =
            new DataContractSerializer(typeof(Dictionary<string, string>));
        serializer.WriteObject(stream, initParams);


Community Additions

© 2015 Microsoft