How to Show a Page if Application is Launched for the First Time

后端 未结 2 1942
花落未央
花落未央 2020-12-18 16:35

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 s

2条回答
  •  隐瞒了意图╮
    2020-12-18 16:58

    You'd better to use the power of UriMapper

    Here you can find a good article.

    The core idea is:

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

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

提交回复
热议问题