Windows10-development-adaptive-code
Windows 10開発-アダプティブコード
この章では、Windows 10でサポートされるさまざまなデバイスへのアプリケーションの採用について説明します。 UIと、UWPアプリケーションで使用されるすべてのトリック、テクニック、およびコントロールの採用については、既に学習しました。
次に、コードの採用について学習します。なぜなら、
- アプリケーションコードはすべてのデバイスで同じではありません。
- 特にXboxで使用されるAPIは、モバイルデバイスでは使用できません。 同じことがHoloLensなどにも当てはまります。
*Adaptive* コードは、アプリケーションを条件付きで点灯させ、特定のデバイスファミリおよび/またはプラットフォーム/拡張APIの特定のバージョンで実行している場合にのみコードを実行できます。
コードを書く
Windows 10では、C ++、C#、Visual Basic、またはJavaScriptを使用して、Visual StudioでUWPアプリケーションを実装できます。
- C#とVisual Basicを使用すると、UI設計にXAMLを使用できます。
- C ++では、XAMLを使用する代わりにDirectXを使用できます。
- JavaScriptの場合、クロスプラットフォームWeb標準であるプレゼンテーションレイヤーにHTMLを使用できます。
Windows Core APIは、コードとUIに必要なほとんどの機能を含むすべてのデバイスに対して同じ方法で実行されます。 ただし、特定のデバイスファミリに合わせて調整されたコードとUIの場合は、適応コードと適応UIを使用する必要があります。
ターゲットデバイスファミリによって実装されていないAPIの呼び出し-
UIはさまざまな画面に簡単に適応しますが、さまざまなデバイスファミリにはさまざまな画面サイズがあるだけでなく、それ以上の機能があります。
- たとえば、携帯電話にはBackやCameraなどのハードウェアボタンがありますが、PCなどの他のデバイスでは使用できない場合があります。
- 既定では、コアAPIにはほとんどの機能が含まれており、すべてのデバイスで機能しますが、外部アセンブリと同様にUWPアプリケーションで拡張SDKを参照することでデバイス固有の機能を使用できます。
アプリケーションに必要な特定の拡張SDKを追加するには、以下の手順に従ってください-
*[参照]* を右クリックします。
* *「参照の追加...」*を選択します。 次のダイアログが開きます。
- 拡張機能の追加は、プロジェクト参照を追加するのと同じくらい簡単です。
- これで、リストから任意の拡張SDKを追加できます。これには、デスクトップ拡張、IoT拡張、モバイル拡張などが含まれます。
デスクトップとモバイルの拡張機能は、最も一般的な2つのプラットフォーム拡張機能SDKです。 たとえば、モバイル拡張機能は、ハードウェアカメラボタンを使用するために必要なAPIを有効にします。
デバイスの機能を確認するには、 Windows.Foundation.Metadata.ApiInformation クラスメソッドを使用します。このメソッドは、現在のデバイスでタイプがサポートされている場合にブール出力を返します。 たとえば、Windowsアプリで次のようなコードでカメラボタンを使用できるようにすることができます-
bool isHardwareButtonsAPIPresent =
Windows.Foundation.Metadata.ApiInformation.
IsTypePresent("Windows.Phone.UI.Inpu t.HardwareButtons");
if (isHardwareButtonsAPIPresent) {
Windows.Phone.UI.Input.HardwareButtons.CameraPressed += HardwareButtons_CameraPressed;
}
電話カメラボタンコードは、デバイスでMobile Extension SDKが有効になっている場合にのみ実行されます。 同様に、以下に示すように、 IsTypePresent の代わりに IsEventPresent 、 IsMethodPresent 、 IsPropertyPresent を使用して、現在のAPIバージョンの特定のイベント、メソッド、またはプロパティを確認することもできます。
bool isHardwareButtons_CameraPressedAPIPresent =
Windows.Foundation.Metadata.ApiInformation.IsEventPresent
("Windows.Phone.UI.Input.HardwareButtons", "CameraPressed");
UWPのWin32 API
C ++/CXで記述されたUniversal Widows Platform(UWP)アプリケーションまたはWindowsランタイムコンポーネントは、現在UWPの一部でもあるWin32 APIにアクセスできます。 すべてのWindows 10デバイスファミリは、アプリケーションを Windowsapp.lib とリンクすることにより、Win32 APIを実装できます。
*Windowsapp.lib* は、UWP APIのエクスポートを提供する「傘」ライブラリです。 *Windowsapp.lib* にリンクすると、すべてのWindows 10デバイスファミリに存在する *dlls* へのアプリの依存関係が追加されます。
アプリケーションがデスクトップと電話の両方をターゲットとする簡単な例を見てみましょう。 したがって、アプリケーションをデスクトップで実行すると、ステータスバーは表示されませんが、同じアプリケーションを電話で実行すると、ステータスバーが表示されます。
以下に、さまざまなコントロールが追加されたXAMLコードを示します。
<Page
x:Class = "UWPAdoptiveCode.MainPage"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local = "using:UWPAdoptiveCode"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable = "d">
<Page.Background>
<SolidColorBrush Color = "Green"/>
</Page.Background>
<Page.BottomAppBar>
<CommandBar x:Name = "commandBar" >
<AppBarButton Icon = "Accept" Label = "appbarbutton"/>
<AppBarButton Icon = "Cancel" Label = "appbarbutton"/>
</CommandBar>
</Page.BottomAppBar>
<Grid Background = "AliceBlue">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<local:DeviceFamilyTrigger DeviceFamily = "Desktop"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target = "StatusBarControls.Visibility"
Value = "Collapsed"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackPanel HorizontalAlignment = "Left" Margin = "75,164,0,0"
VerticalAlignment = "Top" >
<RadioButton x:Name = "ShowAppBarRadioButton" Content = "Show AppBar"
HorizontalAlignment = "Stretch" VerticalAlignment = "Stretch"
IsChecked = "True" Checked = "RadioButton_Checked"/>
<RadioButton x:Name = "ShowOpaqueAppBarRadioButton"
Content = "Show Transparent AppBar" HorizontalAlignment = "Stretch"
VerticalAlignment = "Stretch" Checked = "RadioButton_Checked"/>
<RadioButton x:Name = "HideAppBarRadioButton" Content = "Hide AppBar"
HorizontalAlignment = "Stretch" VerticalAlignment = "Stretch"
Checked = "RadioButton_Checked"/>
</StackPanel>
<StackPanel x:Name = "StatusBarControls" Orientation = "Vertical"
Margin = "75,350,0,0" Visibility = "Visible">
<CheckBox x:Name = "StatusBarBackgroundCheckBox"
Content = "Set StatusBar Background"
Checked = "StatusBarBackgroundCheckBox_Checked"
Unchecked = "StatusBarBackgroundCheckBox_Unchecked"/>
<CheckBox x:Name = "StatusBarHiddenCheckBox"
Content = "Set StatusBar Hidden" Checked = "StatusBarHiddenCheckBox_Checked"
Unchecked = "StatusBarHiddenCheckBox_Unchecked"/>
</StackPanel>
</Grid>
</Page>
以下に、さまざまなイベントのC#実装を示します。
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
//The Blank Page item template is documented at
http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace UWPAdoptiveCode {
///<summary>
///An empty page that can be used on its own or navigated to within a Frame.
///</summary>
public sealed partial class MainPage : Page {
private Color? DefaultTitleBarButtonsBGColor;
private Color? DefaultTitleBarBGColor;
public MainPage() {
this.InitializeComponent();
//Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().
VisibleBoundsCh anged += MainPage_VisibleBoundsChanged;
var viewTitleBar = Windows.UI.ViewManagement.ApplicationView.
GetForCurrentView().TitleBar;
DefaultTitleBarBGColor = viewTitleBar.BackgroundColor;
DefaultTitleBarButtonsBGColor = viewTitleBar.ButtonBackgroundColor;
}
private void RadioButton_Checked(object sender, RoutedEventArgs e) {
//Bottom AppBar shows on Desktop and Mobile
if (ShowAppBarRadioButton != null) {
if (ShowAppBarRadioButton.IsChecked.HasValue &&
(ShowAppBarRadioButton.IsChecked.Value == true)) {
commandBar.Visibility = Windows.UI.Xaml.Visibility.Visible;
commandBar.Opacity = 1;
} else {
commandBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
}
if (ShowOpaqueAppBarRadioButton != null) {
if (ShowOpaqueAppBarRadioButton.IsChecked.HasValue &&
(ShowOpaqueAppBarRadioButton.IsChecked.Value == true)){
commandBar.Visibility = Windows.UI.Xaml.Visibility.Visible;
commandBar.Background.Opacity = 0;
} else{
commandBar.Background.Opacity = 1;
}
}
}
private void StatusBarHiddenCheckBox_Checked(object sender, RoutedEventArgs e){
//StatusBar is Mobile only
if (Windows.Foundation.Metadata.ApiInformation.
IsTypePresent("Windows.UI.ViewManag ement.StatusBar")){
var ignore = Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync();
}
}
private void StatusBarHiddenCheckBox_Unchecked(object sender, RoutedEventArgs e){
//StatusBar is Mobile only
if (Windows.Foundation.Metadata.ApiInformation.
IsTypePresent("Windows.UI.ViewManag ement.StatusBar")){
var ignore = Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ShowAsync();
}
}
private void StatusBarBackgroundCheckBox_Checked(object sender, RoutedEventArgs e){
//StatusBar is Mobile only
if (Windows.Foundation.Metadata.ApiInformation.
IsTypePresent("Windows.UI.ViewManag ement.StatusBar")){
Windows.UI.ViewManagement.StatusBar.GetForCurrentView().
BackgroundColor = Windows.UI.Colors.Blue;
Windows.UI.ViewManagement.StatusBar.GetForCurrentView().
BackgroundOpacity = 1;
}
}
private void StatusBarBackgroundCheckBox_Unchecked(object sender, RoutedEventArgs e){
//StatusBar is Mobile only
if (Windows.Foundation.Metadata.ApiInformation.
IsTypePresent("Windows.UI.ViewManag ement.StatusBar")){
Windows.UI.ViewManagement.StatusBar.GetForCurrentView().
BackgroundOpacity = 0;
}
}
}
public class DeviceFamilyTrigger : StateTriggerBase{
//private variables
private string _deviceFamily;
//Public property
public string DeviceFamily {
get {
return _deviceFamily;
}
set{
_deviceFamily = value;
var qualifiers = Windows.ApplicationModel.Resources.Core.ResourceContext.
GetForCurrentView().Qua lifierValues;
if (qualifiers.ContainsKey("DeviceFamily"))
SetActive(qualifiers["DeviceFamily"] == _deviceFamily);
else
SetActive(false);
}
}
}
}
上記のコードがコンパイルされ、モバイルで実行されると、次のウィンドウが表示されます。
画像に示すように、チェックボックスを使用してステータスバーの背景色を変更できます。
ステータスバーを非表示にすることもできます。
これで、デスクトップデバイスで同じアプリケーションを実行すると、ステータスバーとステータスバーに固有のチェックボックスが表示されていない次のウィンドウが表示されます。