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

dea*_*vmc 5 c# isolatedstorage windows-phone-7

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

Joe*_*nez 6

我不认为这有内置功能...但我知道你的意思:-)我在开源可汗学院为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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 而不是将文件写入IsolatedStorage,而不是更容易使用IsolatedStorageSettings?写一个空白文件听起来有点hacky.http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragesettings(v=vs.95).aspx和http://www.dreamincode.net/forums/topic/198869-wp7 -using-isolatedstoragesettings /有关IsolatedStorageSettings的更多信息. (10认同)