快速入門:應用程式資料漫遊 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

了解如何從應用程式資料漫遊存放區儲存與抓取設定和檔案。 如需應用程式資料漫遊存放區及為什麼要使用它的詳細資訊,請參閱應用程式資料漫遊

登錄以在漫遊資料變更時收到通知

這個範例會將 datachangeHandler 設定成處理漫遊資料變更的處理常式。


var applicationData = Windows.Storage.ApplicationData.current;
 
function initialize() 
{
    applicationData.addEventListener("datachanged", datachangeHandler);
}

function dataChangeHandler(eventArgs)
{
    // TODO: Refresh your data
}

取得應用程式各項設定和檔案的容器

使用 ApplicationData.roamingSettings 屬性來取得設定,而使用 ApplicationData.roamingFolder 屬性來取得檔案。

var roamingSettings = applicationData.roamingSettings;
var roamingFolder = applicationData.roamingFolder;

下一步驟會使用此步驟的 roamingSettingsroamingFolder 變數。

將資料寫入設定

使用 ApplicationDataContainer.values 屬性來存取上一個步驟中取得之 roamingSettings 容器的設定。這個範例會建立名為 exampleSetting 的設定以及名為 HighPriority 的設定,後者適用於轉換具有時間關鍵的資訊,例如應用程式狀態。

// Simple setting

roamingSettings.values["exampleSetting"] = "Hello World";
// High Priority setting, for example, last page position in book reader app

roamingSettings.values["HighPriority"] = "65";

ApplicationDataCompositeValue 物件包含的設定,在存取時不可分開進行。這個範例會建立名為 exampleCompositeSetting 的複合設定,然後將它新增到 roamingSettings 容器。

// Composite setting

var composite = new Windows.Storage.ApplicationDataCompositeValue();
composite["intVal"] = 1;
composite["strVal"] = "string";

roamingSettings.values["exampleCompositeSetting"] = composite;

呼叫 ApplicationDataContainer.CreateContainer 方法來建立設定容器。這個範例會建立名為 exampleContainer 的設定容器,然後新增名為 exampleSetting 的設定。來自 ApplicationDataCreateDisposition 列舉的 Always 值,指出如果還沒有容器,就會建立一個容器。

變更應用程式資料漫遊存放區中的設定後,作業系統會傳送 datachanged 事件。

// Setting in a container

var container = roamingSettings.createContainer("exampleContainer", 
                                                Windows.Storage.ApplicationDataCreateDisposition.Always);

if (roamingSettings.containers.hasKey("exampleContainer"))
{
    roamingSettings.containers.lookup("exampleContainer").values["exampleSetting"] = "Hello World";
}

從設定讀取資料

使用 ApplicationDataContainer.values 屬性來取得 roamingSettings 容器中的 exampleSetting 設定。

// Simple setting

var value = roamingSettings.values["exampleSetting"];
        
if (!value)
{
    // No data
}
else
{
    // Access data in value
}

使用 ApplicationDataContainer.values 屬性來取得 roamingSettings 容器中的 exampleCompositeSetting 設定。

// Composite setting

var composite = roamingSettings.values["exampleCompositeSetting"];

if (!composite)
{
    // No data
}
else
{
    // Access data in composite["intVal"] and composite["strVal"]
}

使用 ApplicationDataContainer.values 屬性來取得 exampleContainer 容器中的 exampleSetting 設定。

// Setting in a container

var hasContainer = roamingSettings.containers.hasKey("exampleContainer");

if (hasContainer)
{
    // Access data in roamingSettings.containers.lookup("exampleContainer").values.hasKey("exampleSetting");
}

將資料寫入檔案

使用檔案 API,例如 Windows.Storage.StorageFolder.createFileAsyncWindows.Storage.FileIO.writeTextAsync,在應用程式資料漫遊存放區中建立和更新檔案。這個範例會在 roamingFolder 容器中建立名為 dataFile.txt 的檔案,然後在這個檔案中寫入目前的日期與時間。來自 CreationCollisionOption 列舉的 replaceExisting 值,指出檔案如果已經存在,就會取代它。

function writeTimestamp() {
   roamingFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
      .then(function (sampleFile) {
         var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
         var timestamp = formatter.format(new Date());

         return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp);
      }).done(function () {      
      });
}

從檔案讀取資料

使用檔案 API (例如,Windows.Storage.StorageFolder.getFileAsyncWindows.Storage.StorageFile.GetFileFromApplicationUriAsyncWindows.Storage.FileIO.readTextAsync),在應用程式資料漫遊存放區中開啟和讀取檔案。這個範例會開啟上一個步驟中建立的 dataFile.txt 檔案,然後讀取該檔案的日期。CreationCollisionOption 列舉的 openIfExists 值表示檔案必須存在。如需從各種位置載入檔案資源的詳細資料,請參閱如何載入檔案資源

function readTimestamp() {
   roamingFolder.getFileAsync("dataFile.txt")
      .then(function (sampleFile) {
         return Windows.Storage.FileIO.readTextAsync(sampleFile);
      }).done(function (timestamp) {
         // Data is contained in timestamp
      }, function () {
         // Timestamp not found
      });
}

當設定不再需要時將其刪除

當您不再需要 exampleSetting 設定時,呼叫 ApplicationDataContainerSettings.remove 方法,從 roamingSettings 容器中將其刪除。

// Simple setting

roamingSettings.values.remove("exampleSetting");

當您不再需要 exampleCompositeSetting 複合設定時,呼叫 ApplicationDataCompositeValue.remove 方法,從 roamingSettings 容器中將其刪除。

// Delete composite setting

roamingSettings.values.remove("exampleCompositeSetting");

當您不再需要 exampleContainer 設定容器時,呼叫 ApplicationDataContainer.deleteContainer 方法將其刪除。

// Delete container

roamingSettings.deleteContainer("exampleContainer");

備註

每個應用程式都有一個應用程式資料漫遊的配額。檢查 ApplicationData.roamingStorageQuota 屬性,判斷允許的漫遊資料大小總計。如果您的漫遊資料超過配額,必須在低於配額之後,才會繼續漫遊。

相關主題

工作

如何載入檔案資源

快速入門:本機應用程式資料

快速入門:暫時的應用程式資料

概念

使用 Windows 執行階段存取應用程式資料

指導方針

應用程式資料漫遊的指導方針

參考

Windows.Storage.ApplicationData

Windows.Storage.ApplicationDataCompositeValue

Windows.Storage.ApplicationDataContainer

Windows.Storage.ApplicationDataContainerSettings

範例

應用程式資料範例