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

Mat*_*hew 1 c# windows-phone-7 windows-phone-8

我想知道如何表示是否第一次启动应用程序,或者之前已经启动了应用程序.我想这样做的原因是在使用应用程序之前显示非常简短的信息性消息,而每隔一次启动应用程序都没有显示.我会在App.xaml.cs中放置如下内容

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

如果(!settings.Contains("WasLaunched")导航到"第一个启动页面"而不是"主页面"?有人能指出我对这个实现的任何好的参考?

编辑**

我将WMAppManifest.xml默认页面更改为LaunchPage.xaml

<DefaultTask Name="_default" NavigationPage="LaunchPage.xaml" />
Run Code Online (Sandbox Code Playgroud)

并创建了我的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是如何相应地更改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?
}
Run Code Online (Sandbox Code Playgroud)

Ant*_*kov 7

你最好使用UriMapper的强大功能

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

核心思想是:

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

然后在应用程序初始化时,您应该定义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();
    }
}
Run Code Online (Sandbox Code Playgroud)