How to use InitParams in Silverlight
Sometimes it is required to "feed" the application with initial parameters. This section describes how to pass intital data via HTML into a Silverlight application
The vast differences between Silverlight and HTML make Silverlight content seem more like an interactive image as opposed different varied tags defined in text. Despite the differences, there is a certain level of coexistence between HTML and Silverlight. Sometimes data needs to be passed between them. This is very useful when it is required to reuse some Silverlight content across multiple pages and pass in some custom values without creating a new Silverlight control nor introducing something more heavy weight like a custom XML file to store application-specific data.
Sometimes it is required to "feed" the application with initial parameters. This section describes how to pass intital data via HTML into a Silverlight application.
To do this, there are “InitParams". These parameters store the given data in key-value pairs, e.g. Key=value, key1=value1... keyN=valueN. Here is an example where initParams are passed to the Silverlight object using the "param" tag of "Object" :
name="initParams" value="val=Foo,color=Blue"
In the above example, two initParams are passed named val and color having values Foo and Blue respectively.
Modifying App.xaml.cs
Once the parameters have been specified in the HTML, they need to be loaded into the Silverlight application. For this, the code is written in App.xaml.cs. The Application_Startup method is present in App.xaml.cs. By default, it should contains just one line of code, setting the RootVisual to a new Page instance:
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page();
}
Modify the Application_Startup() method and add the lines given below:
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page();
if (e.InitParams != null)
{
foreach (var data in e.InitParams)
this.Resources.Add(data.Key, data.Value);
}
}
This code has to go in the above file and method; and cannot be placed anywhere else. That is because the StartupEventArgs is what the InitParams are passed in through, and the StartupEventArgs only exists on the application startup event handler Application_Startup(). The Application_Startup() event handler only exists on classes derived from Application, such as the App.xaml.cs.
Accessing the Resource
The final step is to access this data. With the data now inside App, retrieving it is a bit more straightforward. Just ensure the page that requires this data has fully loaded (using the Loaded event) and then access the resources from App.
An example is given below:
public Page()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
string color = App.Current.Resources["customColor"].ToString();
string value = App.Current.Resources["customValue"].ToString();
}
If this code is not specified in an event handler for the Loaded event, an error may occur where the code in the App.xaml.cs has not fully run. This will cause the application in (in this case) Page.xaml.cs to run the code for accessing the App's resources without actually having the resource loaded. By accessing the resources after Loaded has fired, it ensures that all resources have been loaded for use.