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

时间:2013-10-07 15:45:09

标签: 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);
}

如果(!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?
}

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:3)

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