应用程序首次启动时如何显示页面 [英] How to Show a Page if Application is Launched for the First Time

查看:29
本文介绍了应用程序首次启动时如何显示页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何表示应用程序是第一次启动还是之前已经启动.我想这样做的原因是在应用程序被使用之前显示一个非常短的信息性消息,而每隔一次应用程序启动时什么都不显示.我会在 App.xaml.cs 中放置如下内容吗

I am wondering how to signal whether an appication is launched for the very first time, or has already been launched before. The reason I want to do this is to show a very short informative message before the application is ever used, while every other time the application is launched nothing shows. Would I place something in App.xaml.cs like the following

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

如果 (!settings.Contains("WasLaunched") 导航到第一个启动页面"而不是主页"?有人可以向我指出有关此实现的任何好的参考资料?

And if (!settings.Contains("WasLaunched") navigate to the 'first launch page' as opposed to the 'main page'? Can someone point me to any good references on this implementation?

编辑**

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

I changed my WMAppManifest.xml default page to LaunchPage.xaml

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

并创建了我的 UriMapper 类

And created my UriMapper class

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

But how do I change App.xaml.cs accordingly

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

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

核心思想是:

您应该定义一个空页面 (EntryPage.xaml) 并将其设置为应用的默认页面.然后在您的自定义 UriMapper 中重载 MapUri 方法.

You should define an empty page (EntryPage.xaml) and set it as a default page of your app. Then in your custom UriMapper you overload the MapUri method.

   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:

Then on app initialization you should define which UriMapper to use:

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

这篇关于应用程序首次启动时如何显示页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆