2012-04-04 26 views
3

我有一個代碼在隔離空間中添加了電子郵件ID和名稱。但它無法添加多個數據。另外,如果輸入的數據不正確,我該如何更新?更新隔離存儲器中的數據

namespace IsoStore 
{ 

    public partial class MainPage : PhoneApplicationPage 
    { 

     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
      IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings; 
     } 


     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      IsolatedStorageSettings.ApplicationSettings.Add("email", "[email protected]"); 
      IsolatedStorageSettings.ApplicationSettings.Add("name", "myname"); 
     } 

     private void button2_Click(object sender, RoutedEventArgs e) 
     { 
      textBlock1.Text = (string)IsolatedStorageSettings.ApplicationSettings["email"]; 
      textBlock2.Text = (string)IsolatedStorageSettings.ApplicationSettings["name"]; 
     } 
    } 
} 
+0

你是什麼意思的多個數據? – 2012-04-04 05:27:23

+0

我想保存,例如10個電子郵件ID和名稱。 – user1222006 2012-04-06 00:23:02

+0

aha,現在我得到了你。 – 2012-04-06 04:26:44

回答

3

盪滌你的代碼一點點對你來說,使用一個輔助方法做專賣店:

namespace IsoStore 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     private IsolatedStorageSettings _appSettings; 

     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
      _appSettings = IsolatedStorageSettings.ApplicationSettings;     
     } 


     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      SaveSetting("email", "[email protected]"); 
      SaveSetting("name", "myname"); 
     } 

     private void button2_Click(object sender, RoutedEventArgs e) 
     { 
      textBlock1.Text = (string)_appSettings["email"]; 
      textBlock2.Text = (string)_appSettings["name"]; 
     } 

     private void SaveSetting(string setting, string value) 
     { 
      if (_appSettings.Contains(setting)) 
      { 
       _appSettings[setting] = value; 
      } 
      else 
      { 
       _appSettings.Add(setting, value); 
      } 
     } 
    } 
} 

嘗試一些其他的例子讓你的頭部周圍使用IsolatedStorageSettings。

+0

以及如何檢索這些數據? – Mansinh 2012-09-05 04:42:44

+0

@Mansinh:創建另一個幫助程序來返回字符串(如果它們不存在,則爲null) – 2012-09-06 07:30:54

1

我心裏有2種選擇,您或者您的數據保存到isolatedStorageFile MSDN Library OR,這是我可以在這樣的情況下做的,你保存密鑰郵件下所有的電子郵件作爲一個字符串將電子郵件與不允許在電子郵件中的字符分開,昏迷「,」可以說,在需要時分割字符串並將其檢索到任何讓你感到舒服的東西。

private void SaveSetting(string setting, string value) 
    { 
     if (_appSettings.Contains(setting)) 
     { 
      _appSettings[settings] = _appSettings[settings] + "," + value; 
     } 
     else 
     { 
      _appSettings.Add(setting, value); 
     } 
    } 

請注意,這段代碼是從HiTech Magic的答案中複製過來的。

+0

如果同一封電子郵件保存兩次,則不是非常有用...還可以從另一個答案(包括my * settings * typo)中剪切並粘貼參考,只是添加一個微小的修改,被認爲是抄襲... – 2012-04-06 16:36:45

+0

我認爲最終目標是互相幫助,無論如何,我的道歉。我複製了你的答案,因爲這是我會做的,我可以刪除答案,如果你喜歡它。重複的條目可以輕鬆處理。還有另一種選擇! – 2012-04-06 18:14:03

+0

幫助不是問題。引用您從別人複製的任何內容或您在本網站上遇到的問題。謝謝。 – 2012-04-06 23:09:01