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

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

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

private static bool hasSeenIntro; /// 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. 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; } } 

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

有时,如果版本发生变化,我们需要对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 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); } }