【问题标题】:How to Show a Page if Application is Launched for the First Time首次启动应用程序时如何显示页面
【发布时间】:2014-02-14 12:52:38
【问题描述】:

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

【问题讨论】:

    标签: c# windows-phone-7 windows-phone-8


    【解决方案1】:

    你最好使用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();
        }
    }
    

    【讨论】:

    • 谢谢,我听从了您的建议,并在上面编辑了我最初的问题。我现在在App.xaml.cs 中不确定如何根据应用程序是首次加载还是定期加载来确定导航到哪个页面?
    • 效果很好!我注意到,在点击 WelcomePage 然后导航到实际的 MainPage 之后,WelcomePage 仍然存在于后台堆栈中。我是否只需在 MainPage 中的 OnNavigatedTo 事件上进行检查,然后相应地从后台堆栈中删除该页面?在这种情况下,您认为最好的方法是什么?
    【解决方案2】:

    检查的最佳方法是在隔离存储中写入您当前的状态。要重定向到适当的页面,我个人会使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      相关资源
      最近更新 更多