Today we are going to take a look at a handy way to pass in information to a silverlight application when a web page loads - the initParams. initParams are very similar to flash vars (except, of course, flash vars are for flex and flash apps). I find them very useful for passing in information like callbacks and paths - in fact in the silverlight tutorial the other day I used them to pass in the url for the callback php page.
The information for initParams is set in the html that creates the silverlight application, as one of the param tags inside the silverlight object tag. You can have as many parameters as you want, and they show up inside the silverlight application as a dictionary of string/value pairs. But we will get to exactly how to set them and read them in a moment - first, take a look at the silly example below:
This little silverlight app uses an initParam to determine "my favorite color." So first, lets look at the html:
type="application/x-silverlight-2"
width="400" height="200">
<param name="source" value="InitParamsTest.xap"/>
<param name="minRuntimeVersion" value="2.0.31005.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value="favColor=Green"/>
<a href="http://go.microsoft.com/fwlink/?LinkID=124807">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181"
alt="Get Microsoft Silverlight"/>
</a>
</object>
That is the standard object tag for a Silverlight application, with one small difference - the initParams param tag. The value of the param tag with the name initParams is what gets passed into the silverlight application. In this case, there will be one entry in the initParams dictionary - the key "favColor" holding the value "Green".
Ok, so now we are passing the data into the silverlight app. Now it is time to look at how you get to this data inside the application. When you create a new Silverlight application, Visual Studio will create four files for you: App.xaml, App.xaml.cs, Page.xaml, and Page.xaml.cs. Generally, all that we care about are the two Page files. But to get to the initParams, we have to crack open App.xaml.cs. It should look something like this:
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
InitializeComponent();
}
private void Application_Startup(object sender,
StartupEventArgs e)
{
this.RootVisual = new Page();
}
private void Application_Exit(object sender, EventArgs e)
{
}
}
In particular, we care about the Application_Startup method (which is called when the Application Startup event is fired). The event args for this event (the StartupEventArgs) have a property called InitParams. This property is a dictionary of all the key/value pairs passed in through the initParams param tag.
Unfortunately, as far as I know, this is the only place to access it (unlike flash, where flash vars are stored as publicly accessible statics). Which means that if you need any of the information, you have to grab it now. Generally, what I will do is pass the dictionary straight into the Page being instantiated, like so:
StartupEventArgs e)
{
this.RootVisual = new Page(e.InitParams);
}
This requires changing the Page constructor to accept the dictionary:
{
public Page(IDictionary<string, string> initParams)
{
InitializeComponent();
}
}
For the example above, there wasn't much more beyond that:
{
public Page(IDictionary<string, string> initParams)
{
InitializeComponent();
_TextField.Text = "My Favorite Color is: " + initParams["favColor"];
_TextField2.Text = "";
foreach (string key in initParams.Keys)
_TextField2.Text += key + ": " + initParams[key] + "\n";
}
}
And here is the corresponding xaml:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="200">
<Grid x:Name="LayoutRoot" Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock x:Name="_TextField" TextAlignment="Center"
Grid.Row="0" />
<TextBlock FontWeight="Bold" Grid.Row="1">
All of the initParam fields:
</TextBlock>
<TextBox x:Name="_TextField2"
IsReadOnly="True" Grid.Row="2"/>
</Grid>
</UserControl>
You might be wondering now how to pass in multiple parameter. Well, it is as easy as separating the key/value pairs with commas like so:
type="application/x-silverlight-2"
width="400" height="200">
<param name="source" value="InitParamsTest.xap"/>
<param name="minRuntimeVersion" value="2.0.31005.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value=
"favColor=Blue,callbackUrl=http://abc.def.com/mycallback.php,myOtherInfo=42"/>
<a href="http://go.microsoft.com/fwlink/?LinkID=124807">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181"
alt="Get Microsoft Silverlight"/>
</a>
</object>
Using that html, you get the following:
That about covers initParams for Silverlight. You can grab the Visual Studio solution here - and as always, leave any questions or comments below.
01/07/2009 - 18:33
Thanks for the write up, one question I have is how to dynamically pass the values to initparams? Instead of passing favcolor=green, pass a variable to it instead that could be changed in code behind.
04/13/2009 - 15:23
Did you ever get this solved? I'm wanting to do the same thing and it seems like we're the only ones - or it's the biggest kept secret on the web :)
04/13/2009 - 15:58
ugh.
I was a newbie doofus and caught myself up in .InitParameters.Insert(0, "key1=value1,key2=value2");
instead of .InitParameters = "key1=value1,key2=value2";
06/23/2009 - 15:21
As far as I can tell initParams are only used in silverlight upon loading the application so even resetting dynamically seems to have no effect.
(Course I may have just failed at resetting it in my attempts.)
Anyway what I ended up doing involved calling C# code from javascript which can be pretty complicated so look up "calling managed code from javascript" in your local search engine. This should enable a similar effect.
Basically by doing that I was able to dynamically change the video in my silverlight video player based upon the user clicking on some images. Took me awhile though, and I'm not experienced enough to provide a good walkthrough.
Currently I'm looking for a way to set initParams via information from an xml file in a way that IE will accept. It's one of the seemingly rare conditions that works in Firefox but not in Internet Explorer.
01/12/2009 - 00:33
Hey Tallest, if initParams are separated by commas and equal signs, how do I get those into my values? Are there any escape mechanisms?
01/12/2009 - 09:15
When I was researching for this article, I looked for that answer - and as far as I can tell, there is no escaping mechanism. None of the documentation mentioned anything, and I tried a couple of the normal methods and none of them worked. It kind of surprised me.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.