Friday, August 28, 2009

Passing Parameters to Silverlight 3 Application from ASP.NET

Steve has excellent article here

However, For starters, I thought I would put down the steps quickly here

1. biggest change and where I struggled, How to create a parameter in ASP.NET page, the control is gone. Well, is here to help us. In the Object Tag, add line  -



2. in Code behind in ASPX, use this control to set the property on any event. 2 things to remember here, Use the ".Text" to set the key value pair like this.
Source.Text ="Param1="+ Param1.ToString();

3. In App.xaml.cs, in Application_Startup event, simply add  2 lines, and your total code should look like
this.RootVisual = new MainPage();
foreach (var data in e.InitParams)
   this.Resources.Add(data.Key, data.Value);

4. In MainPage.xaml.cs you want to access this, Simply use following construct.
App.Current.Resources["Param1"].ToString();
Note - Create a Loaded even in MainPage.xaml otherwise we will get. Final code should look like


public MainPage()

{    InitializeComponent();
    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}


void MainPage_Loaded(object sender, RoutedEventArgs e)
{     string param 1 = App.Current.Resources["Param1"].ToString();
}