How to supply start parameters for Silverlight application in querystring

In my article Supply Parameters in InitParams I explained how you can make use of InitParams. But a lot of time, like traditional web applications, we want to specify start parameters or want to drive behavior of page based on some parameters specified in query string. Silverlight application does allow you to do so. Well it is not something specific to Silverlight application. You can access query string any time in your application using Document object of HtmlPage. Following code snippet shows how in my application I switch initial navigation of my application to specific page based on nav query string parameter.


private void Application_Startup(object sender, StartupEventArgs e)
{
if (HtmlPage.Document.QueryString.ContainsKey("nav"))
{
 string navAction = HtmlPage.Document.QueryString["nav"];
 switch(navAction)
 {
  case "Products":
  // Switch navigation to Products page
  break;
  case "Service":
  // SWitch navigation to Services page.
  break;
  default:
  // Switch navigation to default page
  break;
 }
}
this.RootVisual = new Page();
}

Unlike QueryString property in ASP.Net, you can not access querystring parameters by Item accessor. If you will try to access a querystring parameter using following code snippet, KeyNotFoundException exception will be thrown if that parameter is not present in query string.

comments powered by Disqus

Search

Social

Weather

28.6 °C / 83.5 °F

weather conditions Clear

Monthly Posts

Blog Tags