Welcome to DetlefGrohs.com Sign in | Join | Help

Silverlight Different Start Pages

The startup page for a Silverlight application is determined by the RootVisual that is set in the App.xaml file and I wanted a way to have one XAP file with multiple pages that I want to display on pages. My quick solution is to use the InitParameters to pass in a parameter Page that will specify which page that I want to display.

   1: <asp:Silverlight ID="Xaml1" runat="server" InitParameters="Page=Page2" 
   2:                 Source="~/ClientBin/MySilverLightApp.xap" 
   3:                 MinimumVersion="2.0.31005.0" Width="100%" Height="100%" />

Then I access the Page parameter to determine which page to create:

   1: private void Application_Startup(object sender, StartupEventArgs e)
   2: {
   3:     if (e.InitParams.ContainsKey("Page"))
   4:     {
   5:         switch(e.InitParams["Page"])
   6:         {
   7:             case "Page2":
   8:                 this.RootVisual = new Page2();
   9:                 break;
  10:             default:
  11:                 this.RootVisual = new Page();
  12:                 break;
  13:         }            
  14:     }
  15:     else
  16:         this.RootVisual = new Page();            
  17: }

If the Page parameter is not specified or does not match one of the pages that we specify, we will default to the Page.xaml page that is created by default. I know that I could reduce the line count above by removing the default case, but you may want to specify an error page if the parameter is incorrect.

Published Friday, April 17, 2009 5:39 PM by detlef
Filed Under:

Comments

No Comments
Anonymous comments are disabled