Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "first run" flag in WP7

I would like to know if there is a "first run" flag or similar in WP7. My app takes some stuff out of isolated storage so I would like to determine if this is necessary first time. I am currently using an if to check if the named storage object exists but this means I can't handle any memory loss errors in the way I would like.

like image 622
deanvmc Avatar asked Jan 05 '11 21:01

deanvmc


3 Answers

I don't think there is a built in feature for this ... but I know what you mean :-) I implemented "first run" myself using iso storage in the open source khan academy for windows phone app. All I do is look in iso storage for a very small file (I just write one byte to it) ... if it's not there, it's the first time, if it is there, the app has been run more than once. Feel free to check out the source and take my implementation if you'd like :-)

    private static bool hasSeenIntro;

    /// <summary>Will return false only the first time a user ever runs this.
    /// Everytime thereafter, a placeholder file will have been written to disk
    /// and will trigger a value of true.</summary>
    public static bool HasUserSeenIntro()
    {
        if (hasSeenIntro) return true;

        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!store.FileExists(LandingBitFileName))
            {
                // just write a placeholder file one byte long so we know they've landed before
                using (var stream = store.OpenFile(LandingBitFileName, FileMode.Create))
                {
                    stream.Write(new byte[] { 1 }, 0, 1);
                }
                return false;
            }

            hasSeenIntro = true;
            return true;
        }
    }
like image 152
Joel Martinez Avatar answered Sep 28 '22 08:09

Joel Martinez


As @HenryC suggested in a comment on the accepted answer I have used IsolatedStorageSettings to implement "First Run behaviour", here is the code:

    private static string FIRST_RUN_FLAG = "FIRST_RUN_FLAG";
    private static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

    public bool IsFirstRun()
    {
        if (!settings.Contains(FIRST_RUN_FLAG))
        {
            settings.Add(FIRST_RUN_FLAG, false);
            return true;
        }
        else
        {
            return false;
        }
    }
like image 38
nsacerdote Avatar answered Sep 28 '22 08:09

nsacerdote


Sometimes we need to perform some action on every update from Windows store if there is version change. Put this code in your App.xaml.cs

    private static string FIRST_RUN_FLAG = "FIRST_RUN_FLAG";
    private static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

   private static string _CurrentVersion;

    public static string CurrentVersion
    {
        get
        {
            if (_CurrentVersion == null)
            {
                var versionAttribute = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).FirstOrDefault() as AssemblyFileVersionAttribute;
                if (versionAttribute != null)
                {
                    _CurrentVersion = versionAttribute.Version;
                }
                else _CurrentVersion = "";
            }

            return _CurrentVersion;

        }

    }

    public static void OnFirstUpdate(Action<String> action)
    {
        if (!settings.Contains(FIRST_RUN_FLAG))
        {
            settings.Add(FIRST_RUN_FLAG, CurrentVersion);
            action(CurrentVersion);
        }
        else if (((string)settings[FIRST_RUN_FLAG]) != CurrentVersion) //It Exits But Version do not match
        {  
            settings[FIRST_RUN_FLAG] = CurrentVersion;
            action(CurrentVersion);

        }

    }
like image 45
Divesh Pal Avatar answered Sep 28 '22 07:09

Divesh Pal