當應用安裝時,系統會為設定和檔案等應用資料提供它自己的每使用者資料存放區。我們不需要知道這 些資料存在哪裡或如何儲存,因為系統會負責管理實體儲存體工作。我們只需使用應用資料API就可以了 。
本地應用資料一般用於當前裝置資料的持久化,並且本機資料沒有限制大小,通常情況使用 本機資料儲存大型資料集。
如何擷取應用的設定和檔案容器
1.使用 ApplicationData.LocalSettings屬性可以擷取ApplicationDataContainer 對象中的設定。
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
2.使用 ApplicationData.LocalFolder 屬性可以擷取StorageFolder 對象中的檔案。
Windows.Storage.ApplicationDataContainer localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
如何將資料寫入設定
我們可 以通過三種方式將資料寫入設定。
1.使用ApplicationDataContainer.Values屬性。
localSettings.Values["exampleSetting"] = "Hello Windows";
使用鍵-值對的方式。
2.使用ApplicationDataCompositeValue對象,進 行一個複合的設定。
Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue(); composite["intVal"] = 1; composite["strVal"] = "string"; localSettings.Values["exampleCompositeSetting"] = composite;
3.使用 ApplicationDataContainer.CreateContainer方法建立設定容器,將資料添加到容器中。
Windows.Storage.ApplicationDataContainer container = localSettings.CreateContainer("exampleContainer", Windows.Storage.ApplicationDataCreateDisposition.Always); if (localSettings.Containers.ContainsKey("exampleContainer")) { localSettings.Containers["exampleContainer"].Values["exampleSetting"] = "Hello Windows"; }