快速構建Windows 8風格應用26-本地應用資料

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   strong   

原文:快速構建Windows 8風格應用26-本地應用資料

本篇博文主要介紹如何擷取應用的設定和檔案容器、如何將資料寫入設定、如何從設定中擷取資料、如何刪除設定中資料、如何將資料寫入檔案、如何從檔案中擷取資料。

 

當應用安裝時,系統會為設定和檔案等應用資料提供它自己的每使用者資料存放區。我們不需要知道這些資料存在哪裡或如何儲存,因為系統會負責管理實體儲存體工作。我們只需使用應用資料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";
}

其中Windows.Storage.ApplicationDataCreateDisposition的枚舉值Always表示該容器不存在的話進行建立。

如何從設定中擷取資料

1.使用ApplicationDataContainer.Values屬性擷取資料。

Object value = localSettings.Values["exampleSetting"];

 

2.使用ApplicationDataContainer.Values屬性擷取複合設定中資料。

Windows.Storage.ApplicationDataCompositeValue composite = 
   (Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["exampleCompositeSetting"];
 
if (composite == null)
{   
}
else
{   
}

 

3.使用ApplicationDataContainer.Values屬性擷取容器中資料

bool hasContainer = localSettings.Containers.ContainsKey("exampleContainer");
bool hasSetting = false;
 
if (hasContainer)
{
   hasSetting = localSettings.Containers["exampleContainer"].Values.ContainsKey("exampleSetting");
}
如何刪除設定中資料

1.使用ApplicationDataContainerSettings.Remove方法可以刪除資料、複合資料設定以及容器設定。

localSettings.Values.Remove("exampleSetting");
如何將資料寫入檔案

通常我們會使用Windows.Storage.StorageFolder.CreateFileAsyncWindows.Storage.FileIO.WriteTextAsync在本機資料儲存中建立或更新檔案。

async void WriteTimestamp()
{
   Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = 
       new Windows.Globalization.DatetimeFormatting.DateTimeFormatter("longtime");
 
   StorageFile sampleFile = await localFolder.CreateFileAsync("dataFile.txt", 
       CreateCollisionOption.ReplaceExisting);
   await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTime.Now));
}

其中CreationCollisionOption中的ReplaceExisting值表示若該檔案不存在就建立,若存在就替換。

如何從檔案中擷取資料

通常我們會使用Windows.Storage.StorageFolder.GetFileAsyncWindows.Storage.StorageFile.GetFileFromApplicationUriAsyncWindows.Storage.FileIO.ReadTextAsync在本機資料儲存中開啟或讀取檔案。

async void ReadTimestamp()
{
   try
   {
      StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");
      String timestamp = await FileIO.ReadTextAsync(sampleFile);     
   }
   catch (Exception)
   {      
   }
}

 

MSDN中提供相關範例程式碼:Application data sample。

快速構建Windows 8風格應用26-本地應用資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.