Partager via


How to: Specify and Retrieve Custom Initialization Parameters

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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.

Example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
<head>
    <title>SilverlightApplication</title>
    <script type="text/javascript" src="Silverlight.js"></script>
</head>
<body>
    <table>
    <tr><td>
        <object id="slPlugin1" width="300" height="50"
            data="data:application/x-silverlight-2," 
            type="application/x-silverlight-2" >
            <param name="source" value="ClientBin/SilverlightApplication.xap"/>
            <param name="initParams" 
                value="id=slPlugin1,embeddingTechnique=objectElement"/>
            <!-- Installation HTML omitted. -->
        </object>
    </td></tr>
    <tr><td>
        <div id="silverlightControlHost">
            <script type="text/javascript">
                Silverlight.createObject(
                    "ClientBin/SilverlightApplication.xap",  
                    silverlightControlHost, "slPlugin2",
                    { width: "300", height: "50", background: "white" }, { },
                    "id=slPlugin2,embeddingTechnique=createObject" );
            </script>
        </div>
    </td></tr>
    </table>
</body>
</html>
Private Sub Application_Startup(ByVal o As Object, _
    ByVal e As StartupEventArgs) Handles Me.Startup

    Dim p As New Page
    Me.RootVisual = p

    ' This assumes that Page.LayoutRoot exists and is a StackPanel.
    Dim layoutRoot As StackPanel = p.LayoutRoot

    ' Display the custom initialization parameters.
    For Each key As String In e.InitParams.Keys
        layoutRoot.Children.Add(New TextBlock With { _
            .Text = String.Format( _
                "from InitParams: {0} = {1}", _
                key, e.InitParams(key))})
    Next

    ' Display the URL parameters.
    For Each key As String In HtmlPage.Document.QueryString.Keys
        layoutRoot.Children.Add(New TextBlock With { _
            .Text = String.Format( _
                "from QueryString: {0} = {1}", key, _
                HtmlPage.Document.QueryString(key))})
    Next

End Sub
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, 
                e.InitParams[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, 
                HtmlPage.Document.QueryString[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.

Dim initParams As IDictionary(Of String, String)

' When running outside the browser, retrieve initParams from isolated storage.
If Application.Current.IsRunningOutOfBrowser Then
    Using file As IsolatedStorageFile =
        IsolatedStorageFile.GetUserStoreForApplication()
        Using stream As IsolatedStorageFileStream =
            New IsolatedStorageFileStream("initParams.txt",
                                          System.IO.FileMode.Open, file)

            ' The serializer requires a reference to System.Runtime.Serialization.dll.
            Dim serializer As New DataContractSerializer(
                GetType(Dictionary(Of String, String)))
            initParams = CType(serializer.ReadObject(stream), 
                Dictionary(Of String, String))
        End Using
    End Using

' Otherwise, save initParams to isolated storage.
Else
    initParams = e.InitParams

    Using file As IsolatedStorageFile =
        IsolatedStorageFile.GetUserStoreForApplication()
        Using stream As New IsolatedStorageFileStream(
            "initParams.txt", System.IO.FileMode.Create, file)

            Dim serializer As New DataContractSerializer(
                GetType(Dictionary(Of String, String)))
            serializer.WriteObject(stream, initParams)
        End Using
    End Using
End If

MessageBox.Show(String.Concat(initParams))
IDictionary<String, String> initParams;

// When running outside the browser, retrieve initParams from isolated storage.
if (Application.Current.IsRunningOutOfBrowser)
{
    using (IsolatedStorageFile file =
        IsolatedStorageFile.GetUserStoreForApplication())
    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.
else
{
    initParams = e.InitParams;

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

MessageBox.Show(String.Concat(initParams));