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?
}
4

2 回答 2

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

然后在应用程序初始化时,您应该定义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();
    }
}
于 2013-10-07T15:59:33.027 回答
3

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

  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>
于 2013-10-07T16:07:20.473 回答