2013-06-21 52 views
0

我正在創建一個應用程序,其中使用兩次輸入值(starthour,startminute,endhour,endminute)。我寫了一個函數來保存值,然後檢查值並將值放入文本框中。然而,它不工作,我不知道爲什麼。我假設我的錯誤,但我不完全確定。下面的代碼:ApplicationData.LocalSettings不存儲數據?

 public async Task savedata() 
    { 
     while (true) 
     { 
      var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; 

      localSettings.Values["starthour1"] = starthour1.Text; 
      localSettings.Values["starthour2"] = starthour2.Text; 
      localSettings.Values["starthour3"] = starthour3.Text; 
      localSettings.Values["starthour4"] = starthour4.Text; 
      localSettings.Values["starthour5"] = starthour5.Text; 
      localSettings.Values["starthour6"] = starthour6.Text; 
      localSettings.Values["starthour7"] = starthour7.Text; 

      localSettings.Values["startminute1"] = startminute1.Text; 
      localSettings.Values["startminute2"] = startminute2.Text; 
      localSettings.Values["startminute3"] = startminute3.Text; 
      localSettings.Values["startminute4"] = startminute4.Text; 
      localSettings.Values["startminute5"] = startminute5.Text; 
      localSettings.Values["startminute6"] = startminute6.Text; 
      localSettings.Values["startminute7"] = startminute7.Text; 

      localSettings.Values["endhour1"] = endhour1.Text; 
      localSettings.Values["endhour2"] = endhour2.Text; 
      localSettings.Values["endhour3"] = endhour3.Text; 
      localSettings.Values["endhour4"] = endhour4.Text; 
      localSettings.Values["endhour5"] = endhour5.Text; 
      localSettings.Values["endhour6"] = endhour6.Text; 
      localSettings.Values["endhour7"] = endhour7.Text; 

      localSettings.Values["endminute1"] = endminute1.Text; 
      localSettings.Values["endminute2"] = endminute2.Text; 
      localSettings.Values["endminute3"] = endminute3.Text; 
      localSettings.Values["endminute4"] = endminute4.Text; 
      localSettings.Values["endminute5"] = endminute5.Text; 
      localSettings.Values["endminute6"] = endminute6.Text; 
      localSettings.Values["endminute7"] = endminute7.Text; 

      //get data 
      Object starthour1o = localSettings.Values["starthour1"]; 

      if (starthour1o == null) 
      { 
       // No data 
      } 
      else 
      { 
       starthour1.Text = starthour1o.ToString(); 
      } 
      Object starthour2o = localSettings.Values["starthour2"]; 

      if (starthour2o == null) 
      { 
       // No data 
      } 
      else 
      { 
       starthour2.Text = starthour2o.ToString(); 
      } 
      Object starthour3o = localSettings.Values["starthour3"]; 

      if (starthour3o == null) 
      { 
       // No data 
      } 
      else 
      { 
       starthour3.Text = starthour3o.ToString(); 
      } 
      Object starthour4o = localSettings.Values["starthour4"]; 

      if (starthour4o == null) 
      { 
       // No data 
      } 
      else 
      { 
       starthour4.Text = starthour4o.ToString(); 
      } 
      Object starthour5o = localSettings.Values["starthour5"]; 

      if (starthour5o == null) 
      { 
       // No data 
      } 
      else 
      { 
       starthour5.Text = starthour5o.ToString(); 
      } 
      Object starthour6o = localSettings.Values["starthour6"]; 

      if (starthour6o == null) 
      { 
       // No data 
      } 
      else 
      { 
       starthour6.Text = starthour6o.ToString(); 
      } 
      Object starthour7o = localSettings.Values["starthour7"]; 

      if (starthour7o == null) 
      { 
       // No data 
      } 
      else 
      { 
       starthour7.Text = starthour7o.ToString(); 
      } 

      await Task.Delay(10); 
     } 
    } 
+0

當你說「它不工作」,它是如何工作的,是否拋出錯誤,值是否永遠不會保存,程序是否鎖定? –

+0

這是我能找到的唯一文檔:http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localsettings.aspx –

+0

該應用運行良好,但如果我輸入數字(比如2)並關閉應用程序,當我重新打開應用程序2不在那裏。 –

回答

1

兩件事情你需要做的,首先你需要明確地保存你的設置以使它們通過調用Save()被持久化。在你的代碼中的某處,你需要做localSettings.Save(),它應該工作。

第二,如果您保存了設置,您的代碼所做的第一件事就是用文本框的當前值覆蓋它們,那麼需要將localSettings.Values["Foo"] = Foo.Text的整個頂部部分移到底部。

作爲一個方面的評論,你真的需要更新你的代碼每10毫秒?這會在你的程序中吞噬掉大量的資源。更正常的方法是在啓動時加載這些值,然後在關機時保存它們。

+0

好吧,它不會讓我把localSettings.Save()。它說保存沒有定義。我做了#2。而對於#3,這是一個非常早的構建,我現在只是在嘗試。 –

+0

我剛剛放棄你使用[這個類](http://msdn.microsoft.com/en-US/library/windows/apps/windows.storage.applicationdatacontainer)而不是[這個類](http:// msdn。 microsoft.com/en-us/library/system.configuration.applicationsettingsbase.aspx)。你正在使用Windows應用程序的東西。我不知道該如何保存。 –

+0

好的,謝謝。 –