一、漫遊應用資料概覽
1.若應用當中使用了漫遊應用資料,使用者可以很輕鬆的在不同的裝置間保持應用資料的同步。
2.Windows會將更新的漫遊資料同步到雲端,並將資料更新到其他安裝此應用的裝置上。
3.Windows 限制了每個應用可漫遊的應用資料大小。若我們的應用達到漫遊資料大小的限制,在應用的總漫遊資料再次少於該限制之前,不會將應用的任何資料更新到雲端,因此最好的做法是僅為使用者喜好設定、連結和小型資料檔案使用漫遊資料。
4.只要使用者在所需的時間間隔內從某個裝置訪問應用的漫遊資料,這些資料將存在雲端,若使用者不在比此時間間隔更長的時間內運行應用,漫遊資料將從雲端刪除,若使用者卸載應用,漫遊資料不會自動從雲中刪除,將會保留,若使用者在該時間間隔內重新安裝該應用,會從雲端同步漫遊資料。注意:時間間隔指定為 30 天。
5.Windows會隨機漫遊應用資料,並不保證即時同步。若使用者離線或位於高延遲網路中,則漫遊可能會明顯延遲。那麼對於時間關鍵的重要設定,可使用優先順序特別高的設定單位提供更為頻繁的更新。它僅限於名稱為“HighPriority”的一個特定設定單位。它可以是複合體,但總大小限於8KB。此限值不是強制性的,當超過此限值時,設定單位或設定複合體將被視為常規設定單位或複合體。
二、如何構建漫遊應用資料
1.註冊漫遊資料發生更改的事件,該事件為DataChanged事件。
void InitHandlers() { Windows.Storage.ApplicationData.Current.DataChanged += new TypedEventHandler<ApplicationData, object>(DataChangeHandler); } void DataChangeHandler(Windows.Storage.ApplicationData appData, object o) { // 更新資料 }
2.擷取應用的設定和檔案容器。通常我們使用ApplicationData.RoamingSettings屬性可以擷取設定和ApplicationData.RoamingFolder屬性可以擷取檔案。
Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; Windows.Storage.ApplicationDataContainer roamingFolder = Windows.Storage.ApplicationData.Current.RoamingFolder;
3.將資料寫入設定。
使用 ApplicationDataContainer.Values屬性進行簡單的資料寫入。
roamingSettings.Values["exampleSetting"] = "Hello World";
使用ApplicationDataCompositeValue對象進行複合設定。
Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue(); composite["intVal"] = 1; composite["strVal"] = "string"; roamingSettings.Values["exampleCompositeSetting"] = composite;