Saturday, September 12, 2009

Passing Parameters to Silverlight 3 Components

When you go one level up from the typical test projects, you come in to a situation where you need to tweak the Silverlight client app based on some sever side logic. For this to work silverlight supports the concept of InitParams. InitParms allows a hosting aspx page to pass parameters to a Silverlight client application. All over the web this process is well explained, except for one little glitch. i.e It's explained for Silverlight 2, not for the latest Silverlight 3. This gap is critical, since a lot has changed from SL2 to SL3. Specifically the removal of <asp:silverlight ../>, <asp:mediaplayer ../> is pretty significant because most of variable passing information in web is related to <asp:silverlight> markup.

In SL 3 instead of <asp:silverlight> an object tag is used


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

<param name="source" value="{XAP_FILE}"/>

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

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

<param name="InitParams" value="showall=true"/>.....


Now the question is how to set InitParms dynamically. See here for InitParam explanations. The previous links discuss InitParam in detail, but fail to provide how it can be passed dynamically (by hosting aspx page). But I found a close to working solution here but it didn't work for some odd reason. Ultimately a link to a document from Silverlight team on specific changes due to <asp:silverlight> component been missing provided me with the working solution.

The solution is to use a Literal control as value and set the Literal text in the server side aspx code.

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

<param name="source" value="{XAP_FILE}"/>

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

<param name="background" value='<asp:Literal id="Color" runat="server"/>' />

<param name="InitParams" value="'<asp:Literal id="InitParams" runat="server"/>'"/>...

In the hosting aspx pages page load method we do the following;
protected void Page_Load(object sender, EventArgs e)
{
Color.Text = "White"; //You can call any server side code here
InitParams.Text = GetUrl(); //Implement any business logic to get url and return it according to InitParam standard, i.e url=urlvalue
}


Now that the InitParam is set you can use it in your silverlight application by initializing them during Application_Start of SL.
private void Application_Startup(object sender, StartupEventArgs e)
{
string broadcastUrl = e.InitParams["bcUrl"];
this.RootVisual = new MainPage(broadcastUrl );

}

Now you can use this dynamically generated value for any logic within the SL application.
eg: Set the source of media element control, suppose you have a media element with id=mplayer
mPlayer.Source = new Uri(broadcastUrl ); //BroadcastUrl variable should be passed somehow to SL app. (May be via constructor as above)

Post a Comment