About Me

My photo
is a software engineer from bangalore.

Blog Archive

Tuesday, March 17, 2009

Silverlight 2 - Using initParams

I found this very neat and simple article about using Initparams while searching how to pass value s to the silverlight application.
It is done by adding a
tag inside the object call of the silverlight application. as shown below.
This is how you call the silverlight application. The line in
bold shows how parameter is to be passed to the application.

<object data="data:application/x-silverlight,"
type="application/x-silverlight-2"
width="400" height="200">
<param name="source" value="InitParamsTest.xap"/>
<param name="minRuntimeVersion" value="2.0.31005.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value="favColor=Green"/>
<a href="http://go.microsoft.com/fwlink/?LinkID=124807">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181"
alt="Get Microsoft Silverlight"/>
a>
object>



We now edit the App.xaml.cs to match our requirements like shown below.

public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;

InitializeComponent
();
}

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

private void Application_Exit(object sender, EventArgs e)
{

}
}

and in the page.xaml.cs file,

public partial class Page : UserControl
{
public Page(IDictionary<string, string> initParams)
{
InitializeComponent
();
_TextField.
Text = "My Favorite Color is: " + initParams["favColor"];

_TextField2.
Text = "";
foreach (string key in initParams.Keys)
_TextField2.
Text += key + ": " + initParams[key] + "\n";
}
}


The link to the
Original Article......

No comments:

Post a Comment