Forward parameters from Silverlight Web project to Silverlight project

In “real-world” is really common that your Silverlight application needs some parameters from the relatieve Web project.
If you have the necessity to pass some parameters, never mind.
You can use this trick.
For istance, you need to pass the HttpContex from the Silverlight Web to the relatieve Silverlight project.
You can add in default.aspx page in SL Web this tag:

<param name="initParams" value="username=<%=HttpContext.Current.User.Identity.Name%>" />

Then, you need to add in App.xaml.vb page (inside the constructor) this code

If Not e.InitParams Is Nothing Then
      For Each data As Object In e.InitParams
          Me.Resources.Add(data.Key, data.Value)
End If

In this way you add this resources to the Resources dictionary in SL project.
Then, at the end, you can retrieve the resource key property to obtain the relatieve value

Dim user As String = App.Current.Resources("username").ToString()

If you need to pass more than one parameter, add in the same tag “initParams” all the parameters you need and separate them with commas.
For example,

<param name="initParams" value="param1=value1,param2=value2" />

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s