User Tools

Site Tools


notes:uwp:roaming

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
notes:uwp:roaming [2017/01/30]
admin
notes:uwp:roaming [2017/01/30]
admin
Line 27: Line 27:
  
  
-Example: Store and read a high priority ​setting. Windows roams the high priority ​setting as quickly as possible ​(applicable only on desktops, not mobile):+Example: Store and read a //​HighPriority// ​setting. Windows roams the //​HighPriority// ​setting as quickly as possible. The //​HighPriority//​ setting is available ​on Windows Desktop but has no effect on Windows Mobile. 
 <code csharp> <code csharp>
 ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;​ ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;​
Line 54: Line 55:
  
 <code csharp> <code csharp>
 +using Windows.Storage;​
 +...
 public sealed partial class MainPage : Page public sealed partial class MainPage : Page
 { {
Line 59: Line 62:
     {     {
         this.InitializeComponent();​         this.InitializeComponent();​
 +    }
  
-        ​Windows.Storage.ApplicationData.Current.DataChanged += +    protected override void OnNavigatedTo(NavigationEventArgs args) 
-            new TypedEventHandler<​ApplicationData,​ object>(OnDataChanged);+    { 
 +        ​Windows.Storage.ApplicationData.Current.DataChanged += OnDataChanged;​ 
 +    } 
 +     
 +    protected override void OnNavigatedFrom(NavigationEventArgs args) 
 +    { 
 +        Windows.Storage.ApplicationData.Current.DataChanged -= OnDataChanged;
     }     }
  
     // OnDataChanged is invoked whenever roaming data changes.     // OnDataChanged is invoked whenever roaming data changes.
     private void OnDataChanged(ApplicationData appData, object args)     private void OnDataChanged(ApplicationData appData, object args)
 +    {
 +        Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,​
 +            () => UpdateFromSettings());​
 +    }
 +
 +    private void UpdateFromSettings()
     {     {
         ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;​         ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;​
Line 83: Line 99:
         }         }
     }     }
- +    ​ 
-    // A custom method to change the roaming settings+    // For testing
-    ​// Call it for example from a Button_Click event handler. +    ​private ​void TestButton_Click(object senderRoutedEventArgs e)
-    public ​void UpdateSettings(string test1int test2)+
     {     {
         ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;​         ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;​
  
-        roamingSettings.Values["​TestStr"​] = test1+        ​// Change the roaming settings. 
-        roamingSettings.Values["​TestInt"​] = test2;+        ​roamingSettings.Values["​TestStr"​] = "​Testing"​
 +        roamingSettings.Values["​TestInt"​] = 88;
  
-        // Signal the change ​in the settings.+        // Signal the change.
         ApplicationData.Current.SignalDataChanged();​         ApplicationData.Current.SignalDataChanged();​
-    } 
- 
- 
-    // For testing. 
-    private void TestButton_Click(object sender, RoutedEventArgs e) 
-    { 
-        UpdateSettings("​Testing",​ 888); 
     }     }
 } }
 </​code>​ </​code>​
  
-Notes the book "WinRT via C#": When Windows copies a package'​s roaming data from the cloud onto a user's PC, Windows raises //​ApplicationData//'​s //​DataChanged//​ event. If your app registers with this event, it can refresh its new roaming settings and modify its behavior on the fly while the user is interacting with your app. Note that Windows raises this event on a thread pool thread, so you'll have to use a //​CoreDispatcher//​ if you want to update your app's user interface.+Notes from the book "WinRT via C#": When Windows copies a package'​s roaming data from the cloud onto a user's PC, Windows raises //​ApplicationData//'​s //​DataChanged//​ event. If your app registers with this event, it can refresh its new roaming settings and modify its behavior on the fly while the user is interacting with your app. Note that Windows raises this event on a thread pool thread, so you'll have to use a //​CoreDispatcher//​ if you want to update your app's user interface.
  
 Windows raises the //​DataChanged//​ event automatically whenever the PC downloads new roaming package data. But your app can raise this event itself by calling //​ApplicationData//'​s //​SignalDataChanged//​ method. It may be useful if you need your app to signal a setting change to one of its background tasks or vice versa. Windows raises the //​DataChanged//​ event automatically whenever the PC downloads new roaming package data. But your app can raise this event itself by calling //​ApplicationData//'​s //​SignalDataChanged//​ method. It may be useful if you need your app to signal a setting change to one of its background tasks or vice versa.
notes/uwp/roaming.txt · Last modified: 2017/01/30 by admin