简体   繁体   English

WP7中是否存在“首次运行”标志

[英]Is there a “first run” flag in WP7

I would like to know if there is a "first run" flag or similar in WP7. 我想知道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. 我目前正在使用if来检查命名存储对象是否存在,但这意味着我无法以我想要的方式处理任何内存丢失错误。

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 . 我不认为这有内置功能...但我知道你的意思:-)我在开源可汗学院为windows phone app使用iso存储实现了“首次运行”。 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. 我所做的就是在iso存储器中查找一个非常小的文件(我只写一个字节)...如果它不存在,那是第一次,如果它存在,那么应用程序已经运行了不止一次。 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;
        }
    }

As @HenryC suggested in a comment on the accepted answer I have used IsolatedStorageSettings to implement "First Run behaviour", here is the code: 正如@HenryC在对已接受答案的评论中建议我使用IsolatedStorageSettings来实现“First Run行为”,这里是代码:

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

Sometimes we need to perform some action on every update from Windows store if there is version change. 有时,如果版本发生变化,我们需要对Windows商店的每次更新执行一些操作。 Put this code in your App.xaml.cs 将此代码放在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);

        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM