如果首次启动应用程序,如何显示页面

我想知道如何表示是否第一次启动应用程序,或者之前已经启动了应用程序。 我想这样做的原因是在使用应用程序之前显示非常简短的信息性消息,而每隔一次启动应用程序都没有显示。 我会在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

  

并创建了我的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? } 

你最好使用UriMapper的强大function

在这里你可以找到一篇好文章 。

核心思想是:

您应该定义一个空页面( 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(); } } 

检查的最佳方法是按照您当前的状态在隔离存储中写入状态。 要重定向到适当的页面,我个人会使用URI映射器。 这样,您的第一个预期输入页面将位于导航第一个条目堆栈中,从而阻止用户导航回第一页。 一个典型的用例是当用户未经过身份validation时将用户重定向到身份validation页面,并在用户已经过身份validation时将用户重定向到主页,请参阅此示例

  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