Silverlight
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. 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 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

html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
<!-- 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>
Visual Basic
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
C#
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])
        });
    }            
}

Application extension services can access custom initialization parameters in their IApplicationService..::.StartService method implementations. For more information, see Application Extension Services.

See Also

Concepts

Tags :


Page view tracker