Windows phone 8 在IsolatedStorage设置中声明密钥

Windows phone 8 在IsolatedStorage设置中声明密钥,windows-phone-8,isolatedstorage,Windows Phone 8,Isolatedstorage,我的目标是检索上次应用程序关闭时存储的计数器值。 i、 e.我将计数器值存储在独立存储器中。若计数器的值为5,应用程序关闭并再次启动,我应该能够检索到5。 为此,我写了以下代码,但我无法理解 IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings; int count=0; isoStoreSettings["flag"]; if (isoStoreSettings["flag"

我的目标是检索上次应用程序关闭时存储的计数器值。 i、 e.我将计数器值存储在独立存储器中。若计数器的值为5,应用程序关闭并再次启动,我应该能够检索到5。 为此,我写了以下代码,但我无法理解

IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
int count=0;
isoStoreSettings["flag"];
if (isoStoreSettings["flag"]!="set")
{                
   isoStoreSettings["count"] = count;
   isoStoreSettings["flag"] = "set";
}


count = isoStorageSettings["count"];  //using the value of count stored previously

//some code which updates the count variable

isoStorageSettings["count"]=count;
这段代码的问题是现在允许在isolatedstorage中声明键,我们必须为该键分配一些值 但如果我为该键赋值,它将在每次启动应用程序时重新初始化该键。 所以,如果有人能解决这个问题,请帮忙
即使对于我的目标,除了隔离存储还有其他选择,也请分享。

在添加方法中使用更新的计数值,如下所示:

    IsolatedStorageSettings isolatedStorageSettingsCount = IsolatedStorageSettings.ApplicationSettings;
if (isolatedStorageSettingsCount.Contains("count"))
            {
                isolatedStorageSettingsCount.Remove("count");
                isolatedStorageSettingsCount.Add("count",5);
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            else
            {
                isolatedStorageSettingsCount.Add("count",5);
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
要检索您的计数值,请使用以下代码:

IsolatedStorageSettings isolatedStorageSettingsCount = IsolatedStorageSettings.ApplicationSettings;
string strCount = (string)isolatedStorageSettingsCount["count"];
int count=Convert.ToInt32(strCount);

如果希望每次启动应用程序时加载
计数
,请将代码放入App.xaml.cs中的
应用程序启动
事件:

// declare static variable which you will be able to access from anywhere
public static int count;

private void Application_Launching(object sender, LaunchingEventArgs e)
{
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    if (settings.Contains("count")) count = (int)settings["count"];
    else count = 0;
}
点击事件-保存变量:

private void Application_Closing(object sender, ClosingEventArgs e)
{
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    if (settings.Contains("count")) settings["count"] = count;
    else settings.Add("count", count);
    settings.Save();
}
从代码中的任何地方,您都应该能够访问变量,如下所示:

int myVariable = App.count;
App.count++; 
// and so on
请注意,您还可以考虑
已激活
已停用
事件-有关详细信息,请参阅


我也不知道
flag
应该做什么,但是上面的代码应该可以很好地保存变量。

使用它你会发现它很有用。这段代码也有同样的问题,我应该为第一次分配计数做些什么?我必须用零初始化它,但如果我这样做,它每次都会重新初始化。当它重新初始化你得到的值时,你在哪里使用这段代码?国旗是什么?