Windows10-development-connected-experience
Windows10 Dev-コネクテッドエクスペリエンス
既に知っているように、Windows 10では、複数のWindows 10デバイスで実行および実行できるアプリケーションを作成できます。 これらの異なるデバイスがあり、異なるデバイスで実行されているにもかかわらず、1つのアプリケーションのように感じさせたいとします。
ユニバーサルWindowsプラットフォーム(UWP)では、すべてのWindows 10デバイスで単一のアプリケーションを実行でき、ユーザーにそれが1つのアプリケーションであるという感覚を与えることができます。 これは「接続エクスペリエンス」として知られています。
コネクテッドエクスペリエンスの重要な機能-
- Windows 10は、よりパーソナルコンピューティングの時代への第一歩であり、アプリ、サービス、およびコンテンツをデバイス間でシームレスかつ簡単に移動できます。
- 接続されたエクスペリエンスを使用すると、そのアプリケーションに関連するデータと個人設定を簡単に共有でき、すべてのデバイスで利用できるようになります。
この章では、学習します-
- これらの共有データまたは設定は、その1つのアプリケーションのデバイスで使用できるように保存されます。
- ユーザーの識別方法。異なるデバイスで同じアプリケーションを使用しているのは同じユーザーであること。
Windows 10は大胆な一歩を踏み出しました。 あなたがMicrosoftアカウント(MSA)またはあなたの企業または(仕事)アカウントのいずれかでWindows 10にログインすると、それが仮定されます-
- OneDrive for MSAアカウントに無料でアクセスでき、エンタープライズアカウントのクラウドバージョンであるActive Directory(AD)およびAzure Active Directory(AAD)にアクセスできます。
- さまざまなアプリケーションとリソースにアクセスできます。
- デバイスとアプリケーションはローミング状態と設定になっています。
Windows 10でのローミング
PCにログオンするとき、ロック画面や背景色などの設定をしたり、さまざまな種類の設定をカスタマイズしたりします。 Windows 10で実行されているコンピューターまたはデバイスが複数ある場合、同じアカウントで他のデバイスにログインすると、1つのデバイスの設定と設定がクラウドから同期されます。
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 で背景色の変更情報を正常に取得したことを示しています。