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

时间:2014-03-28 05:30:14

标签: c# windows-phone-8

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

该应用程序是关于一个有计时器的数学游戏 当我完成游戏时,高分更新,即使我向后导航并重新导航到页面,高分领域仍然显示保存的高分,这很棒

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

我的代码:

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;
    }

2 个答案:

答案 0 :(得分:3)

请务必致电"保存"退出前(或者例如离开设置页面时)在您的设置上:

 IsolatedStorageSettings.ApplicationSettings.Save();

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

答案 1 :(得分:2)

更新设置时使用保存方法:

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;
            }