Pages

Friday, October 22, 2010

InitParameters In Silverlight

Hello friends,
Today I am going to discuss about the InitParameters. These are used to pass some initial value from the aspx page to our silverlight page. In some situation there is a need to pass some values to our silverlight page using our calling page of aspx. Silverlight handles this situation using the INITPARAMETERS. Using the InitParameters is very easy.

The value which we have passed using the parameters can be accessed inside the application start-up event of the app.xaml file. Either we can pass the parameter using the XAML or we can also pass these parameters using the code. the below are the sample for Doing so.


By XAML
We can directly pass the parameters in the aspx page from where we have defined our XAP file to be called on that page like.

InitParameters="Name=Anil,Country=INDIA"
Width="500"


Or by the code like this.

MySilverlightPage.InitParameters="Name=Anil,Country=INDIA"


And for receiving these paramter Open the App.Xaml file and look for the below event

private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}

In the Above function use the below code to access the parameters passed by the page.

      string Param1 =   e.InitParams[ "Name" ];
      string Param2 =   e.InitParams[ "Country" ];

We have to make use of the string variables to store the value of the paramters. so the above function will be like this.

private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
         string Param1 =   e.InitParams[ "Name" ];
         string Param2 =   e.InitParams[ "Country" ];
}


Isn't its Simple. I hope You have liked this sample. I will come up with some new samples on WPF and Silverlight. Please post your comment and let me know what else example you would like to see in the next articles..


Thanks
Anil Kumar Pandey
Microsft MVP C# (2009-10,2010-11)
Mumbai, INDIA

No comments:

Post a Comment

Kontera