2014-03-28 271 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; 
      }