2014-03-28 237 views
0

我正在开发中的Windows Phone 8 一个应用程序,我需要一些小的数据存储(排行榜在我的情况)独立存储不存储数据时,应用程序退出

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

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

我的代码:

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

回答

3

确保您退出(或当例如离开设置之前你叫“保存”您的设置页):

IsolatedStorageSettings.ApplicationSettings.Save(); 

你可以叫它每次更改设置的时间,但它建议不要做过于频繁(所以如果你做一组更改,不叫,直到保存 结束)。

2

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