当应用程序退出时,隔离存储不存储数据

本文关键字:存储 数据 隔离 应用程序 退出 | 更新日期: 2023-09-27 17:54:33

我正在开发一个Windows Phone 8的应用程序我需要一些小数据来存储(Hilicore在我的情况下)

这个应用程序是关于一个有计时器的数学游戏当我完成游戏时,高分更新,即使我返回并重新导航到页面,高分字段仍然显示保存的高分,这是伟大的

问题是当我退出应用程序并重新打开它时,高分重置。我不知道为什么

我代码:

IsolatedStorageSettings highScoreSettings = IsolatedStorageSettings.ApplicationSettings;

  public void TimeLeftTick(Object sender, EventArgs args)
        {
            prog1.Value-=10;
              //GAME ENDS
            if (prog1.Value == 0)
            {
                //If there is already a highscore saved 
                if(highScoreSettings.Contains("highscore"))
                if (Score > Convert.ToInt32(highScoreValue.Text))
                { 
                    highScoreSettings.Remove("highscore"); // remove highscore
                    highScoreSettings.Add("highscore", Score.ToString()); // update highscore
                    highScoreValue.Text = highScoreSettings["highscore"].ToString(); 
                }
                MessageBox.Show("Time is out");
                TimeLeft.Stop();
                prog1.Value = 100;
                return;
            }

// LOAD DATA
 private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains("highscore"))
            highScoreValue.Text = IsolatedStorageSettings.ApplicationSettings["highscore"] as string;
    }

当应用程序退出时,隔离存储不存储数据

确保在退出设置之前(或者例如离开设置页面时)调用"Save":

 IsolatedStorageSettings.ApplicationSettings.Save();

您可以在每次更改设置时调用它,但建议不要经常这样做(因此,如果您进行了一组更改,请不要在最后调用save)。

在更新设置时使用Save方法:

IsolatedStorageSettings highScoreSettings = IsolatedStorageSettings.ApplicationSettings;
  public void TimeLeftTick(Object sender, EventArgs args)
        {
            prog1.Value-=10;
              //GAME ENDS
            if (prog1.Value == 0)
            {
                //If there is already a highscore saved 
                if(highScoreSettings.Contains("highscore"))
                if (Score > Convert.ToInt32(highScoreValue.Text))
                { 
                    highScoreSettings.Remove("highscore"); // remove highscore
                    highScoreSettings.Add("highscore", Score.ToString()); 
                    highScoreSettings.Save();
                    highScoreValue.Text = highScoreSettings["highscore"].ToString(); 
                }
                MessageBox.Show("Time is out");
                TimeLeft.Stop();
                prog1.Value = 100;
                return;
            }