正如我们所知,在Windows 10中,我们可以创建一个可以在多个Windows 10设备上执行和运行的应用程序.让我们假设我们有这些不同的设备,我们想让它感觉它是一个应用程序,即使它在不同的设备上运行.
在通用Windows平台(UWP)中,您可以在所有Windows 10设备上运行单个应用程序,并且可以让用户感觉它是一个应用程序.这被称为连接体验.
连接体验和减号的重要功能;
Windows 10是迈向更个性化计算时代的第一步,您的应用,服务和内容可以随时随地跨设备移动.
凭借互联经验,您可以轻松共享与该应用程序相关的数据和个人设置,并且可以在所有设备上使用.
在本章中,我们将学习&减去;
这些共享数据或设置将被存储它可以在您的设备上为该应用程序提供.
如何识别用户;它是在不同设备上使用相同应用程序的同一用户.
Windows 10向前迈出了一大步.当您使用Microsoft帐户(MSA)或企业或(工作)帐户登录到Windows 10时,假定 :
您可以免费访问OneDrive for MSA帐户,并且可以访问Active Directory(AD)和Azure Active Directory(AAD),这是一个包含企业帐户的云版本.
您可以访问不同的应用程序和资源.
设备和应用程序处于漫游状态和设置.
当您登录到PC时,您可以设置一些首选项,如锁定屏幕或背景颜色,或个性化您的不同类型的设置.如果您有多台运行在Windows 10上的计算机或设备,当您使用同一帐户登录其他设备时,您将在云中同步一台设备上的首选项和设置.
在Windows 10中,当您设置或个性化您的应用程序设置时,这些设置将使用UWP中提供的漫游API进行漫游.当您在其他设备上再次运行相同的应用程序时,它将首先检索设置并将这些设置应用于该设备上的应用程序.
将漫游数据上传到云端的限制为100KB.如果此限制超出,则同步将停止,并且只会像本地文件夹一样.
RoamingSettings API公开为字典,应用程序可以在其中保存数据.
Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; // Retrivve value from RoamingSettings var colorName = roamingSettings.Values["PreferredBgColor"].ToString(); // Set values to RoamingSettings roamingSettings.Values["PreferredBgColor"] = "Green";
当 RoamingSettings 中的数据发生变化时,它会触发 DataChanged 事件,您可以在其中刷新设置.
Windows.Storage.ApplicationData.Current.DataChanged += RoamingDataChanged; private void RoamingDataChanged(Windows.Storage.ApplicationData sender, object args) { // Something has changed in the roaming data or settings }
让我们看一个例子,我们将在其中设置应用程序的背景颜色,这些设置将使用UWP中提供的漫游API进行漫游.
下面给出了添加了不同控件的XAML代码.
<Page x:Class = "RoamingSettingsDemo.Views.MainPage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local = "using:RoamingSettingsDemo.Views" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable = "d"> <Grid x:Name = "MainGrid" Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height = "80" /> <RowDefinition /> </Grid.RowDefinitions> <StackPanel Orientation = "Horizontal" VerticalAlignment = "Top" Margin = "12,12,0,0"> <TextBlock Style = "{StaticResource HeaderTextBlockStyle}" FontSize = "24" Text = "Connected Experience Demo" /> </StackPanel> <Grid Grid.Row = "1" Margin = "0,80,0,0"> <StackPanel Margin = "62,0,0,0"> <TextBlock x:Name = "textBlock" HorizontalAlignment = "Left" TextWrapping = "Wrap" Text = "Choose your background color:" VerticalAlignment = "Top"/> <RadioButton x:Name = "BrownRadioButton" Content = "Brown" Checked = "radioButton_Checked" /> <RadioButton x:Name = "GrayRadioButton" Content = "Gray" Checked = "radioButton_Checked"/> </StackPanel> </Grid> </Grid> </Page>
RoamingSettings 的C#实现,下面给出了不同的事件.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // The RoamingSettingsDemo Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 namespace RoamingSettingsDemo.Views { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { SetBackgroundFromSettings(); Windows.Storage.ApplicationData.Current.DataChanged += RoamingDataChanged; } protected override void OnNavigatedFrom(NavigationEventArgs e) { Windows.Storage.ApplicationData.Current.DataChanged -= RoamingDataChanged; } private void RoamingDataChanged(Windows.Storage.ApplicationData sender, object args) { // Something has changed in the roaming data or settings var ignore = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () ⇒ SetBackgroundFromSettings()); } private void SetBackgroundFromSettings() { // Get the roaming settings Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; if (roamingSettings.Values.ContainsKey("PreferBrownBgColor")) { var colorName = roamingSettings.Values["PreferBrownBgColor"].ToString(); if (colorName == "Gray") { MainGrid.Background = new SolidColorBrush(Colors.Gray); GrayRadioButton.IsChecked = true; } else if (colorName == "Brown") { MainGrid.Background = new SolidColorBrush(Colors.Brown); BrownRadioButton.IsChecked = true; } } } private void radioButton_Checked(object sender, RoutedEventArgs e){ if (GrayRadioButton.IsChecked.HasValue && (GrayRadioButton.IsChecked.Value == true)) { Windows.Storage.ApplicationData.Current.RoamingSettings. Values["PreferBrownBgCo lor"] = "Gray"; } else { Windows.Storage.ApplicationData.Current.RoamingSettings. Values["PreferBrownBgCo lor"] = "Brown"; } SetBackgroundFromSettings(); } } }
编译并执行上述代码时,您将看到以下窗口.
让我们选择灰色作为背景颜色并关闭此应用程序.
现在,当您在此设备或任何其他设备上运行此应用程序时,您将看到背景颜色已更改为灰色.这表明该应用已成功检索到 RoamingSettings 中背景颜色变化的信息.