Is there a "first run" flag in WP7
Asked Answered
S

3

5

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.

Selfrevealing answered 5/1, 2011 at 21:30 Comment(0)
V
6

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;
        }
    }
Viscosity answered 5/1, 2011 at 21:33 Comment(3)
clever. you could write a version # instead, so you could detect upgrades, or for apps with trial support, you could also use it to detect an upgrade from trial to paid.Shela
Hi Joel, did you also consider not writing the byte? Wondering if you opted for this based on any observations during testing. In my own brief tests in this area I found FileExists works ok without that.Foul
Instead of writing a file to IsolatedStorage, would it not be easier to use IsolatedStorageSettings instead? Writing a blank file sounds a little hacky. msdn.microsoft.com/en-us/library/… and dreamincode.net/forums/topic/… for more info on IsolatedStorageSettings.Subprincipal
C
4

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;
        }
    }
Conservator answered 20/2, 2014 at 15:43 Comment(0)
S
1

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

        }

    }
Snowdrift answered 15/11, 2014 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.