Go Back

Init Params in Silverlight 3

I converted my silverlight app over to silverlight 3 today and ran into an issue with InitParams because the silverlight hosting control is no longer valid for silverlight 3 nor provided in the vs2008 tools.

 

The quick and dirty: I had to take all of the object tag string and move it to the code behind by inserting into the inner Html of the div tag that hosts it.

 

Explanation:

 

When you create a new Silverlight 3 application it asks you if you want to add it to an existing asp.net application. If you do that it generates a test aspx page that has something like this:

 

    <div id="silverlightControlHost">

        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%"height="100%">

            <param name="source"value="ClientBin/JPeckham.xap"/>

            <param name="onError"value="onSilverlightError"/>

            <param name="background"value="white"/>

            <param name="minRuntimeVersion"value="3.0.40624.0"/>

            <param name="autoUpgrade"value="true"/>

            <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 MicrosoftSilverlight" style="border-style:none"/>

            </a>

          </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>

What you need to do is change the div to be runat=”server” and do a CUT of the object tag contents. You’ll have this then:

<div id="silverlightControlHost"runat="server">

</div>

 

Now inside of your code behind for the page, past that string and parse it using a string builder, like this:

silverlightControlHost.InnerHtml =

silverlightControlHost.InnerHtml =

            new StringBuilder(

            @"<object id=""SilverlightApp"" data=""data:application/x-silverlight-2,""

type=""application/x-silverlight-2"" width=""100%"" height=""100%"" >

              <param name=""source"" value=""../../ClientBin/JPeckham.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=")

 

                                            .Append(BuildSilverlightInitParametersFromQueryString())

           

                                            .Append( @"""/>

              <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>").ToString();

 

NOTE: Basically BuildSilverlightInitParametersFromQueryString()is the method that constructs your InitParams value string. So write thathowever you feel like. Make sure toHtmlEncode it! J

Facebook DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Furl it!

Comments  2

  • Jelgab 9/30/2009 12:00:00 AM

    You can also make the initparams available to the code by using id = "TheParameters" runat = "server":

     

    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" >

      <param name="initparams"        id = "TheParameters" runat = "server" />

      <param name="source"            value="ClientBin/PasoParametros01Silver.xap"/>

      <param name="onError"           value="onSilverlightError" />

      <param name="background"        value="white" />

      <param name="minRuntimeVersion" value="3.0.40624.0" />

      <param name="autoUpgrade"       value="true" />

     

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

     

    And then, from the code, set the value like this:

     

    protected void Page_Load( object sender, EventArgs e )

    {

        TheParameters.Attributes[ "value" ] = "a=1,b=2,AnyParameter=AnyValue";

    }

     

  • james peckham 10/5/2009 12:00:00 AM

    that's even better. I'm glad you posted it!
Post a comment!
  1. Formatting options