Detect app first launch windows phone [duplicate]

2019-09-07 11:15发布

问题:

This question already has an answer here:

  • Recognize when is the app first launched WP8 1 answer

I'm quite new on windows phone dev. I would like to detect when the user launches my app for the first time to display an explaination frame for example by calling :

if(firstLaunch)
showTheFrameToTheGuyBecauseHeLaunchedTheAppForTheFirstTime();

I would be sooooooo glad if someone could show us such a small script...

Thank you in advance guys !

回答1:

I would recommend you to use the builtin applicationsettings.

const string settingsAppLaunched = "appLaunched";

public static bool IsFirstLaunch(){
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    return !(settings.Contains(settingsAppLaunched) && settings[settingsAppLaunched]);
}

public static bool Launched(){
    if(IsFirstLaunch()){
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        settings.Add(settingsAppLaunched, true);
        settings.Save();
    }
}

//usage:
if(IsFirstLaunch()){
    showTheFrameToTheGuyBecauseHeLaunchedTheAppForTheFirstTime();
    Launched();
} 

Microsoft Documentation about Settings in Windows Phone is available here.



回答2:

You can simply use IsolatedStorageSettings.

if(!IsolatedStorageSettings.ApplicationSettings.Contains("first"))
{
   // Do your stuff
   IsolatedStorageSettings.ApplicationSettings["first"] = true;
   IsolatedStorageSettings.ApplicationSettings.Save();
}

This code will do it.