以 Dictionary方式存取 IsolatedStorage

以 Dictionary方式存取 IsolatedStorage

之前有介紹過 IsolatedStorage 目前在 WP7 上有三種方式存取(檔案、Directory 及 LocalDataBase)

今天這篇介紹 以Key-Value (字典)【Dictionary<TKey, TValue>】 方式下資料儲存在IsolatedStorage

我們需要使用一個叫 IsolatedStorageSettings 的 class

namespace: System.IO.IsolatedStorage

 

下面為片段程式

讀取基本資料型態:

 

   1:    if (IsolatedStorageSettings.ApplicationSettings.Contains(userMailKey)) 
   2:    { 
   3:       textUserMail.Text = IsolatedStorageSettings.ApplicationSettings[userMailKey].ToString();
   4:    } 

 

儲存基本資料型態:

   1:  IsolatedStorageSettings.ApplicationSettings[userMailKey] = textUserMail.Text
 
 

 

也可以存取複雜型態:

 

   1:  public class Person {
   2:      public string UserName { get; set; }
   3:      public string Tel { get; set; }
   4:  }

 

   1:  var student = new Person(){UserName="Jerry Li",Tel="09124567"};
   2:   
   3:   
   4:  /*儲存*/
   5:   
   6:  IsolatedStorageSettings.ApplicationSettings["TheStudentKey"] = student;
   7:   
   8:   
   9:   
  10:   
  11:   
  12:  /*讀取*/
  13:   
  14:  var  student= IsolatedStorageSettings.ApplicationSettings[“TheStudentKey”] as Person;