2013-10-07 125 views
1

我想知道如何表示應用程序是第一次啓動還是以前已啓動。我想這樣做的原因是在應用程序被使用之前顯示非常簡短的信息消息,而應用程序每隔一段時間都不會顯示。我將使東西在App.xaml.cs像下面如何在首次啓動應用程序時顯示頁面

var settings = IsolatedStorageSettings.ApplicationSettings; 
if (!settings.Contains("WasLaunched")) 
{ 
    MessageBox.Show("First time to launch"); 
    settings.Add("WasLaunched", true); 
} 

如果(!settings.Contains("WasLaunched")導航到「第一啓動頁」,而不是「主頁」?有人能指出我對這個實現有什麼好的參考嗎?

編輯**

我改變了我的WMAppManifest.xml默認頁LaunchPage.xaml

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

而且創建了UriMapper類

public class LoginUriMapper : UriMapperBase 
{ 
    public override Uri MapUri(Uri uri) 
    { 
     if (uri.OriginalString == "/LaunchPage.xaml") 
     { 
      if (Settings.FirstLoad.Value == true) 
      { 
       //Navigate to Welcome Page with quick first time user info 
       uri = new Uri("/Views/WelcomePage.xaml", UriKind.Relative); 
      } 
      else 
      { 
       ///Navigate to the actual Main Page 
       uri = new Uri("/MainPage.xaml", UriKind.Relative); 
      } 
     } 
     return uri; 
    } 
} 

但是如何更改App.xaml.cs相應

private void Application_Launching(object sender, LaunchingEventArgs e) 
{ 
    //how to check and navigate to correct page for this specific method? 
} 

private void Application_Activated(object sender, ActivatedEventArgs e) 
{ 
    //how to check and navigate to correct page for this specific method? 
} 

回答

7

你最好使用UriMapper

Here you can find a good article的威力。

的核心思想是:

你應該定義一個空頁(EntryPage.xaml)並將其設置爲你的應用程序的默認頁。 然後在您的自定義UriMapper中,您超載了MapUri方法。

public class YourUriMapper : UriMapperBase 
    { 
    public override Uri MapUri(Uri uri) 
    { 
     if (uri.OriginalString == "/EntryPage.xaml") 
     { 
      var settings = IsolatedStorageSettings.ApplicationSettings; 

      if (!settings.Contains("WasLaunched")) 
      { 
       uri = new Uri("/FirstRunInfoPage.xaml", UriKind.Relative); 
      } 
      else 
      { 
       uri = new Uri("/MainPage.xaml", UriKind.Relative); 
      } 
     } 
      return uri; 
    } 
    } 

然後在應用程序初始化,你應該確定要使用的UriMapper

private void Application_Launching(object sender, LaunchingEventArgs e) 
{ 
    RootFrame.UriMapper = new YourUriMapper(); 
} 

private void Application_Activated(object sender, ActivatedEventArgs e) 
{ 
    if (e.IsApplicationInstancePreserved == false) 
    { 
     // tombstoned! Need to restore state 
     RootFrame.UriMapper = new YourUriMapper(); 
    } 
} 
+0

謝謝,我跟着你的意見d編輯了我上面的第一個問題。在App.xaml.cs中,我現在不確定如何根據應用程序第一次加載還是定期加載來確定要導航到哪個頁面? – Matthew

+0

答覆已更新。你可以檢查代碼。 –

+0

工作很好!我注意到,雖然在點擊WelcomePage並導航到實際的MainPage後,WelcomePage仍然存在於後臺堆棧中。我只需要在MainPage的OnNavigatedTo事件中檢查它,然後相應地從後面的棧中移除頁面?你認爲在這種情況下最好的方法是什麼? – Matthew

3

的檢查最好的辦法是寫在隔離存儲設備的狀態,你現在是。要重定向到適當的頁面,我會親自使用一個URI映射器。這樣,您的第一個打算進入的頁面將位於導航的第一個入口堆棧中,防止用戶導航返回到第一頁。一個典型的用例是將用戶重定向到認證頁面,當用戶沒有通過驗證,並在用戶已經通過身份驗證的主頁,看到This example

public App() 
    { 
     SetUpLandingPageView(); 
    } 


    void SetUpLandingPageView() 
    { 
     var isLaunched = IsolatedStorageSettings.ApplicationSettings.Contains("WasLaunched"); 

     // Get the UriMapper from the app.xaml resources, and assign it to the root frame 
     var mapper = Resources["mapper"] as UriMapper; 

     if (mapper == null) 
      throw new ArgumentNullException("Mapper must be configured"); 

     RootFrame.UriMapper = Resources["mapper"] as UriMapper; 

     // Update the mapper as appropriate 
     mapper.UriMappings[0].MappedUri = isLaunched ? new Uri("/Views/HomePage.xaml", UriKind.Relative) : new Uri("/Views/Introduction.xaml", UriKind.Relative);  
    } 

在App.xaml中

命名空間:

xmlns:UriMapper="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone" 

的XAML

<Application.Resources> 
    <ResourceDictionary> 
     <UriMapper:UriMapper x:Name="mapper"> 
      <UriMapper:UriMapping Uri="/MainPage.xaml" /> 
     </UriMapper:UriMapper> 
    </ResourceDictionary> 
</Application.Resources> 
相關問題