In this post, we will look into some of the approaches available for passing values between Silverlight pages: a Silverlight page to a Silverlight page and an ASPX page to a Silverlight page. A typical approach in legacy web systems is using the QueryString
, where parameters are passed as field–value pairs.
QueryString in Silverlight
In a real application scenario, suppose a Customer
page appears when the user clicks on to see the details of a particular Customer
. The customer
ID/name will be used in the query string
and can be passed to the CustomerDetail
page. The CustomerDetail
page will parse the URL and fetch the details from the data source based on the value from the query string
.
Silverlight pages can access the query string
from the NavigationContext
property of the page (check my earlier post for more details). So in the above example, the CustomerDetail
page can access it with one line of code: this.NavigationContext.QueryString["**QS Field Name**"]
.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
lblQSValue.Content = this.NavigationContext.QueryString["PassQS"];
}
Passing Parameters from an ASPX Page to Silverlight Using InitParameters
Although QueryString
is a way to pass values between pages, there are alternate ways of sending parameters to a Silverlight application, one of which is InitParam
.
In an ASPX page, in the object
tag where the XAP file gets loaded, we can add the following tag:
<param name="InitParams"
value="PassVALA = IamA,PassVALB = 25″ />
<body>
<form id="form1″ runat=""server""
style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2″
width="100%" height="100%">
<param name="source"
value="ClientBin/NavigationSystem.xap"/>
<param name="onError"
value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion"
value="4.0.50826.0″ />
<param name="InitParams"
value="PassVALA = IamA,PassVALB = 25″ />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0″
style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376″
alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame"
style="visibility:hidden;height:0px;width:0px;border:0px">
</iframe></div>
</form>
</body>
Now the same parameters can be accessed in the Silverlight application in the Application_Startup
event in App.xaml.