Is there a “first run” flag in WP7

余生颓废 提交于 2019-12-17 16:48:24

问题


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.


回答1:


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



回答2:


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



回答3:


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

        }

    }


来源:https://stackoverflow.com/questions/4609412/is-there-a-first-run-flag-in-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!