InitParams in Silverlight


If you are developing a Silverlight Application, and you need to pass some parameters inside – for example a key and value pair then we can pass the key value pair from the aspx page itself. We will see how we can do this in Silverlight.

Create a Silverlight Project


Figure 1.1 Creating Silverlight Project

Adding parameters

Open the "InitializingParametersTestPage.aspx" and find the tag
tag  <asp:Silverlight  add an attribute InitParameters
Enter the following code to the tag


Defining the Parameters

In App.xaml.cs add an object of IDictionary<string,string> as follows

public IDictionary<string, string> AppParams;
In Application_Startup event initialize the parameters as follows
private void Application_Startup(object sender, StartupEventArgs e)
            AppParams = e.InitParams;
            this.RootVisual = new Page();

Using Parameters

In Page.xaml add ListBoxes to show the parameter values
Xaml Code

<UserControl x:Class="InitializingParameters.Page"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="#FFB7C2E5">
                                <ColumnDefinition Width="0.472*"/>
                                <ColumnDefinition Width="0.025*"/>
                                <ColumnDefinition Width="0.502*"/>
        <ListBox x:Name="myKeysList"/>
        <ListBox x:Name="myValuesList" Grid.Column="2"/>

In code behind of the Page.xaml.cs add the following code to bind the parameters

namespace InitializingParameters
    public partial class Page : UserControl
        public Page()
            App myApp = App.Current as App;

            foreach (string item in myApp.AppParams.Keys)
            foreach (string item1 in myApp.AppParams.Values)

Runnning the Application

When you run the application the list will carry the key and value pairs.


Figure 1.2 Displaying Key Value pair

Hope you like the article, Enjoy Coding.


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now