Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

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

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

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?

EDIT**

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

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

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

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?
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...