2012-02-12 74 views
6

我想要有不同的開始頁面,具體取決於IsolatedStorage中是否存儲了一些設置。如何更改WP7應用程序的啓動頁面

卜我不知道哪裏是最好的做法來處理這個。也就是說,如果我在孤立存儲中找到某些東西,我會讓用戶獲取MainPage,否則我會像用戶一樣獲取設置頁面。

我正在使用MVVM-light,如果有一些神奇的東西要使用。

+0

http://stackoverflow.com/questions/3892271/how-do-i-change-the-startup-page -on-a-wp7-silverlight-app – driis 2012-02-12 13:32:13

回答

9

你可以通過設置虛擬頁面作爲項目的主要頁面做到這一點。您可以通過編輯項目的WMAppManifest.xml文件中更改主頁:

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

現在,檢測對虛擬頁面的所有導航,並重定向到任何你想要的頁面。

要做到這一點,在App.xaml.cs文件,在構造函數的結尾,請訂閱「導航」事件:

this.RootFrame.Navigating += this.RootFrame_Navigating; 

在事件處理中,如果導航定向檢測到虛擬頁面,取消了導航,並重定向到你想要的網頁:

void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e) 
{ 
    if (e.Uri.OriginalString == "/DummyPage.xaml") 
    { 
     e.Cancel = true; 

     var navigationService = (NavigationService)sender; 

     // Insert here your logic to load the destination page from the isolated storage 
     string destinationPage = "/Page2.xaml"; 

     this.RootFrame.Dispatcher.BeginInvoke(() => navigationService.Navigate(new Uri(destinationPage, UriKind.Relative))); 
    } 
} 

編輯

事實上,甚至還有 更輕鬆。在應用程序構造結束,只需設置與更換的URI UriMapper你想:

var mapper = new UriMapper(); 

mapper.UriMappings.Add(new UriMapping 
{ 
    Uri = new Uri("/DummyPage.xaml", UriKind.Relative), 
    MappedUri = new Uri("/Page2.xaml", UriKind.Relative) 
}); 

this.RootFrame.UriMapper = mapper; 
+0

嗨,通過應用程序文件中的獨立存儲來執行操作是否是一種很好的做法? – 2012-02-12 18:57:28

+0

只要你不做冗長的操作,它應該沒問題。訪問應用程序文件中的獨立存儲沒有任何問題,但您應該知道在顯示應用程序的第一頁之前執行此代碼。因此,您只需5秒鐘即可完成計算,否則您的應用將無法通過認證。 – 2012-02-12 19:22:42

+0

Greate,tnx爲關於5秒規則的信息:) 而你的第二個解決方案工作在greate – 2012-02-12 21:30:47