C# 是否有一个“问题”;“第一次运行”;WP7中的标志
我想知道WP7中是否有“首次运行”标志或类似标志。我的应用程序从独立存储中取出一些东西,所以我想确定这是否是第一次需要。我目前正在使用if来检查命名的存储对象是否存在,但这意味着我无法以我希望的方式处理任何内存丢失错误。我认为这没有内置功能。。。但我知道你的意思:-)我自己在开源中使用iso存储实现了“首次运行”。我所做的就是在iso存储中查找一个非常小的文件(我只向其中写入一个字节)。。。如果没有,这是第一次,如果有,应用程序已经运行了不止一次。如果愿意,请随时查看源代码并获取我的实现:-)C# 是否有一个“问题”;“第一次运行”;WP7中的标志,c#,windows-phone-7,isolatedstorage,C#,Windows Phone 7,Isolatedstorage,我想知道WP7中是否有“首次运行”标志或类似标志。我的应用程序从独立存储中取出一些东西,所以我想确定这是否是第一次需要。我目前正在使用if来检查命名的存储对象是否存在,但这意味着我无法以我希望的方式处理任何内存丢失错误。我认为这没有内置功能。。。但我知道你的意思:-)我自己在开源中使用iso存储实现了“首次运行”。我所做的就是在iso存储中查找一个非常小的文件(我只向其中写入一个字节)。。。如果没有,这是第一次,如果有,应用程序已经运行了不止一次。如果愿意,请随时查看源代码并获取我的实现:-)
私有静态bool hasSeenIntro;
///将仅在用户第一次运行此操作时返回false。
///此后,每次都会将占位符文件写入磁盘
///并将触发一个值true。
公共静态bool hasUserSeenNintro()
{
if(hasSeenIntro)返回true;
使用(var store=IsolatedStorageFile.GetUserStoreForApplication())
{
如果(!store.FileExists(LandingBitFileName))
{
//只需写一个一字节长的占位符文件,这样我们就知道它们以前已经登陆过
使用(var stream=store.OpenFile(LandingBitFileName,FileMode.Create))
{
写入(新字节[]{1},0,1);
}
返回false;
}
hasSeenIntro=true;
返回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);
}
}
私有静态字符串FIRST\u RUN\u FLAG=“FIRST\u RUN\u FLAG”;
私有静态隔离存储设置=隔离存储设置。应用设置;
私有静态字符串_CurrentVersion;
公共静态字符串当前版本
{
得到
{
如果(_CurrentVersion==null)
{
var versionAttribute=Assembly.getExecutionGassembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute),true);
if(versionAttribute!=null)
{
_CurrentVersion=versionAttribute.Version;
}
否则_CurrentVersion=“”;
}
返回当前版本;
}
}
公共静态无效OnFirstUpdate(操作)
{
如果(!settings.Contains(第一个运行标志))
{
添加(第一个运行标志,当前版本);
行动(当前版本);
}
else if(((字符串)设置[第一个运行标志]!=CurrentVersion)//它退出,但版本不匹配
{
设置[第一次运行标志]=当前版本;
行动(当前版本);
}
}
聪明。你可以写一个版本,而你可以检测升级,或者试用试用版的应用程序,你也可以用它来检测从试用到付费的升级。嗨,乔尔,你也考虑不写这个字节了吗?不知道您是否根据测试期间的观察结果选择了此选项。在我自己在这方面的简短测试中,我发现FileExists在没有它的情况下可以正常工作。与其将文件写入IsolatedStorage,不如使用IsolatedStorage设置更容易吗?写一个空白文件听起来有点老套。以及有关隔离存储设置的更多信息。
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);
}
}