2013-10-03 18 views
2

我的應用程序目前在啓動時轉到MainPage.xaml(我不知道它在哪裏配置)。如何在啓動時更改起始頁?

我希望能夠在某些條件下從另一個頁面開始。我想我可以在App.xaml.cs頁面將此代碼添加到Application_Launching()

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

NavigationService不提供App.xaml.cs.

如何使用其他頁面啓動應用程序foo == true

回答

4

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); 

} 

Property\WMAppManifest.xml文件中設置靜態啓動頁面

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

編輯

試試:

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

Property\WMAppManifest.xml明確NavigationPage:

<DefaultTask Name ="_default" NavigationPage=""/> 
+0

嗨@nirmus。 ((App)Application.Current中有錯誤).Root..'。它說'MyApplication.App.RootFrame.Get'不能用實例引用訪問。使用類型名稱代替 – user2799350

+0

它必須工作。發佈你的代碼,也許你做了一些其他的錯誤 – nirmus

+0

我只是複製/粘貼你的代碼到應用程序啓動。當我寫'((App)Application.Current)「時,'RootFrame'不會出現在intellisense中。 – user2799350

3

這是根據條件來瀏覽方式:

在App.xaml.cs的構造函數中添加:

RootFrame.Navigating+= RootFrameOnNavigating; 

然後再定義RootFrameOnNavigating是這樣的:

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; 
    } 

另一種方法是使用UriMapper重定義導航到某個頁面時導航到的URI。