Thursday, March 08, 2012

Set start page dynamically in wp7

My new Windows Phone 7 app "WeekCal" provides different calendar views. There are actually two week views and a month view. In the settings, the user can set the view to start with.
To start the app with the view the user has choosen, there are two steps necessary.

First step is to select the right view in the App.xaml.cs file and set it as root frame element. Do this in the Application_Launching method. Here is an example:

 private void Application_Launching(object sender, LaunchingEventArgs e)
 {
   Uri uri;
   // Read the user settings
   var views = IsolatedStorageProvider.LoadViewSettings();
   // Month view
   if (views.ShowMonthView)
   {
     uri = new Uri("/MonthViewPage.xaml", UriKind.Relative);
   }
   else
   {
     // Week view
     if (views.ShowFiveDaysWeek)
     {
       uri = new Uri("/FiveDaysPage.xaml", UriKind.Relative);
     }
     else
     {
       uri = new Uri("/SevenDaysPage.xaml", UriKind.Relative);
     }
   }
   // Set the start page
   ((App)Application.Current).RootFrame.Navigate(uri);
 }  

Everything looks ok, but the app does not start with the right view. There is one more easy step to do. In the WMAppManifest.xml file, the DefaultTask tag needs an empty NavigationPage property. So just delete the default entry MainPage.xaml.

<Tasks>
 <!--This is set in code (App.xaml.cs)-->
 <DefaultTask Name="_default" NavigationPage="" />

That's it, hope that helps.