有没有在WP7“第一跑”标志
-
25-09-2019 - |
题
我想知道是否有一“第一次运行”标志或类似WP7。我的应用程序需要一些东西出来隔离存放的,所以我想确定这是否是必要的第一次。我目前使用的,如果检查,如果指定的存储对象存在,但这意味着我不能处理我想顺便任何记忆丧失的错误。
解决方案
我不认为有一个内置的功能,这...但我知道你的意思:-)我实施“首次运行”采用异存储在开源的汗Windows Phone的应用学院。我要做的就是看在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;
}
}
其他提示
由于@HenryC在我用IsolatedStorageSettings实施“首次运行行为”公认的答案评论认为,这里是代码:
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<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);
}
}
不隶属于 StackOverflow