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="" >
<!-- 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 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))})

    ' 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, _

End Sub

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 =
        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.
    initParams = e.InitParams

    Using file As IsolatedStorageFile =
        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


Community Additions