I was working on a chapter about the Application Model for our forthcoming book and reviewed the examples on passing parameters into Silverlight applications. I thought, "Hey! That would make a great video!" And it does, but Tim already made one, which I highly recommend.
Since it never hurts to review, I’m going to write this up quickly, fully acknowledging that this is 90% his work; so only read on if you haven’t happened to catch his presentation of this material yet and this might be new to you (I have added a few new bits that I learned as I worked this through).
What You’re Trying To Accomplish
The idea is this: you want to be able to create a Silverlight application that allows for passing in some information through the html or aspx page that will be integrated into the Silverlight application at run time. Remembering that Silverlight runs on the client, that information must be picked up just as the application begins to run inside its hosting html or aspx page. Here, briefly, is how you can do that:
If you are hosting in an HTML page, you’ll add a new parameter to the object tag
You’ll find there are already a number of parameters there, you’ll add one whose name is initParams and whose value is any number of name/value pairs that you’d like to pass as parameters to your program. For example,
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="initParams" value="programmer=Jesse Liberty, blog=http://silverlight.net/blogs/jesseliberty" />
The name/value pairs are comma separated and you may have as many as you like,
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="initParams" value="programmer=Jesse Liberty, blog=http://silverlight.net/blogs/jesseliberty", title=silverlight geek, codeRippedOffFrom=tim heuer, conceptDemonstrated=initializing parameters />
In this second example, I’m passing in five initializing parameters. The keys are
- programmer
- blog
- title
- codeRippedOffFrom
- conceptDemonstrated
and their respective values are
- Jesse Liberty
- http://silverlight.net/blogs/jesseliberty
- silverlight geek
- tim heuer
- initializing parameters
You can see that this lends itself readily to being placed in a dictionary, and that is exactly what will happen, they are placed into an IDictionary<string><string> available to you from the Application_Startup method in App.xaml.cs (but only there, and thus only when your application is starting up). You therefore want to grab these values and put them somewhere you can get to them later, when you’ll need them.
Stashing Away The Values
The canonical solution is to place them into the global resources dictionary, thus
private void Application_Startup( object sender, StartupEventArgs e ) { if ( e.InitParams != null ) { foreach ( var item in e.InitParams ) { this.Resources.Add( item.Key, item.Value ); } } this.RootVisual = new Page(); }
Using the parameters
Once the parameters are safely tucked away we can think about using them in the running program. I wrote a small example program that has a TextBlock for a title, a ListBox to display some data and then two rows that display information if that information is passed in through parameters.
The first row will display the programmer’s name if there is a parameter whose key is programmer, and the second will provide a link to the programmer’s blog if there is a parameter whose key is blog. Here is the Xaml that sets that up:
<TextBlock x:Name="Programmer" Grid.Row="1" Grid.Column="1" FontSize="14" Margin="10,0,0,0" /> <HyperlinkButton x:Name="BlogLink" Grid.Row="2" Grid.Column="1" FontSize="14" Margin="10,0,0,0" /> <TextBlock x:Name="BlogText" Grid.Row="2" Grid.Column="1" FontSize="14" Margin="10,0,0,0" />
Notice that both the HyperlinkButton and the TextBlock occupy the same row and column. The first thing we do in the load handler is to make them both invisible,
void Page_Loaded( object sender, RoutedEventArgs e ) { BlogText.Visibility = Visibility.Collapsed; BlogLink.Visibility = Visibility.Collapsed;
We’ll check the resources for the link and if we find one, we’ll make the link visible; if not, we make the textblock visible and put a message there that there is no link.
Retrieving Values from the Resources Collection
Following Tim’s approach, I’ve factored out the job of getting the value out of the Resources given a key, checking first to ensure that the key exists in the resources collection and returning an empty string otherwise,
private string GetParam( string p ) { if ( App.Current.Resources[p] != null ) return App.Current.Resources[p].ToString(); else return string.Empty; }
Note: You might expect to use the TryGetValue method on the Resources collection, which does exactly this work for you, but the Silverlight documentation notes that this is not intended for use in user code with the ResourcesDictionary class. |
With our GetParam method in place, we can try to get the value for the Programmer and the address for the blog, placing them in local (temporary) variables,
void Page_Loaded( object sender, RoutedEventArgs e ) { BlogText.Visibility = Visibility.Collapsed; BlogLink.Visibility = Visibility.Collapsed; string programmer = GetParam( "programmer" ); string blog = GetParam( "blog" ); if ( programmer.Length > 0 ) Programmer.Text = "Programmer: " + programmer; else Programmer.Text = "Programmer unknown";
The next step is to test whether we received a legitimate blog address. If so, we want to make the HyperTextButton visible and set its text and its URI, otherwise we’ll make the TextBlock visible and set its text to some sort of error message,
if ( blog.Length > 0 ) { BlogLink.Content = "Programmer's Blog"; BlogLink.NavigateUri = new System.Uri( blog ); BlogLink.Visibility = Visibility.Visible; } else { BlogText.Text = "No blog provided"; BlogText.Visibility = Visibility.Visible; }
The result is very gratifying, the parameters are integrated into the running Silverlight program seamlessly,
A very nice, and easily overlooked feature. Again, for complete coverage, I recommend Tim’s video and the complete source code for the example shown today is available here.
10 Responses to Passing Parameters Into Silverlight Applications