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

时间:2011-01-05 21:30:28

标签: c# windows-phone-7 isolatedstorage

我想知道WP7中是否存在“首次运行”标志或类似标志。我的应用程序从隔离存储中取出一些东西,所以我想确定第一次是否有必要。我目前正在使用if来检查命名存储对象是否存在,但这意味着我无法以我想要的方式处理任何内存丢失错误。

3 个答案:

答案 0 :(得分:6)

我认为这没有内置功能......但我知道你的意思:-)我在开源khan academy for windows phone app中使用iso存储实现了“首次运行”。我所做的就是在iso存储器中查找一个非常小的文件(我只写一个字节)...如果它不存在,那是第一次,如果它存在,那么应用程序已经运行了不止一次。如果您愿意,请随时查看来源并采取我的实施: - )

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

答案 1 :(得分:4)

正如@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;
        }
    }

答案 2 :(得分:1)

如果版本发生变化,有时我们需要对Windows商店的每次更新执行一些操作。将此代码放在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);

        }

    }