Initial Parameters for Silverlight
When writing Silverlight applications there might one question come up from time to time: Is it possible to "feed" the application with initial parameters and if yes, how can this be done?
To do this, there are so-called InitParams. This paramaters stores the given data in key-value pairs. They can be declared this way:
website=http://blog.norberteder.com,category=wpf
This means:
- Each key-value pair uses a comma as delimiter
- A value can be assigned to the key using =
The declaration of the initial parameters can be done within the parameter tags of the Silverlight object definition:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="ClientBin/DemoApp.xap"/> <param name="onerror" value="onSilverlightError" /> <param name="background" value="white" /> <param name="minRuntimeVersion" value="2.0.31005.0" /> <param name="autoUpgrade" value="true" /> <param name="initparams" value="website=http://blog.norberteder.com,category=wpf" /> <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/> </a> </object>
In the App’s code behind file two events are registered. One of them is the application’s Startup-event. Within the eventhandler you have access to the StartupEventArgs which provides access to the InitParams.
private void Application_Startup(object sender, StartupEventArgs e) { NeededParameters parameters = new NeededParameters(); if (e.InitParams != null && e.InitParams.Count > 0) { if (e.InitParams.ContainsKey("website")) parameters.Website = e.InitParams["website"]; if (e.InitParams.ContainsKey("category")) parameters.Category = e.InitParams["category"]; } this.RootVisual = new Page(parameters); }
The code above shows how you can fill up a custom object (NeededParameters) using the data given bei the Init Params. This object is used as argument for the constructor of the Page to be displayed.
That’s everything you have to do.
November 7th, 2008 at 10:48 am
[…] the rest here: Initial Parameters for Silverlight asp-net, consulting, dotnet-graz, downloads, internet, norbert, norbert-eder, podcasts, […]