How to change the start page at startup?

Currently, my application goes to MainPage.xaml at startup (I don't know where it is configured).

I want to start from a different page in some conditions. I think I can add this code to Application_Launching() in the App.xaml.cs page:

 NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative)); 

but NavigationService not available in App.xaml.cs.

How to start an application from another page if foo == true ?

+2
source share
2 answers

Change the start page in App.Xaml.cs :

 private void Application_Launching(object sender, LaunchingEventArgs e) { Uri nUri = new Uri("/SecondPage.xaml", UriKind.Relative); ((App)Application.Current).RootFrame.Navigate(nUri); } 

Setting up a static launch page in the Property\WMAppManifest.xml file

 <DefaultTask Name ="_default" NavigationPage="SecondPage.xaml"/> 

Edit

Try:

  private void Application_Launching(object sender, LaunchingEventArgs e) { Uri nUri = new Uri("/GamePage.xaml", UriKind.Relative); RootFrame.Navigate(nUri); } 

and in Property\WMAppManifest.xml clear NavigationPage:

 <DefaultTask Name ="_default" NavigationPage=""/> 
+4
source

Here is the navigation method depending on the condition:

In the App.xaml.cs constructor, add:

 RootFrame.Navigating+= RootFrameOnNavigating; 

and then define RootFrameOnNavigating as follows:

  private bool firstNavigation = true; private void RootFrameOnNavigating(object sender, NavigatingCancelEventArgs navigatingCancelEventArgs) { //by defaullt stringOfPageNameSetInWMAppManifest is /MainPage.xaml if (firstNavigation && navigatingCancelEventArgs.Uri.ToString().Contains(stringOfPageNameSetInWMAppManifest)) { if (foo == true) { //Cancel navigation to stringOfPageNameSetInWMAppManifest navigatingCancelEventArgs.Cancel = true; //Use dispatcher to do the navigation after the current navigation has been canceled RootFrame.Dispatcher.BeginInvoke(() => { RootFrame.Navigate(new Uri("/Page1.xaml", UriKind.Relative)); }); } firstNavigation = false; } 

Another way would be to use UriMapper to override what uri is oriented to when you go to a specific page.

+3
source

All Articles