 モバイルアプリ開発のための Core Cross-Platform API
 UI コードの共有方法で二通り
Shared C# App Logic
(.NET Standard)
Shared XAML/C# UI Code
(Xamarin.Forms)
iOS
C# UI
Shared C# App Logic
(.NET Standard)
Android
C# UI
Windows
C# UI
Xamarin.FormsXamarin Native
ロジックのみ共通化
UIはネイティブで個別に作りこむ
 Shared C# App Logic の共通化は?
Shared C# App Logic
(.NET Standard)
 .NET Standard ライブラリのみで実装は不可能
 プラットフォーム固有の API を利用する必要がある
 Interface を作成後、DependencyService / DI Container 経由で呼び出す
Shared C# App Logic
(.NET Standard)
NSUserDefaults SharedPreferences ApplicationData
Interface (C#)
UIPasteboard ClipboardManager Clipboard
CLLocationManager LocationManager Geolocator
アプリ毎に個別実装
していた
 プラットフォーム固有の機能を共通 API で利用できる
ようにした
Shared C# App Logic
(.NET Standard)
NSUserDefaults SharedPreferences ApplicationData
Core Cross-Platform API
Xamarin.Essentials
UIPasteboard ClipboardManager Clipboard
CLLocationManager LocationManager Geolocator
 ユーザー設定
NSUserDefaults SharedPreferences ApplicationData
Preferences.Set(“KeyName”, 100);
Accelerometer
App InformationBarometer
Battery
Clipboard
Color Converters
Compass
Connectivity
Detect Shake
Device Display Information
Device Information
Email
File System Helpers
Flashlight
Geocoding
Geolocation
Gyroscope Launcher
Magnetometer
MainThread
Maps
Open Browser
Orientation Sensor
Phone Dialer
Platform Extensions
Preferences
Secure Storage
Share
SMS
Text-to-Speech
Unit Converters
Version Tracking
Vibrate
# 機能名 説明
1 Accelerometer 加速度計 3 次元空間内のデバイスの加速度データを取得します。
2 App Information アプリ情報 アプリケーションに関する情報を見つけます。
3 Barometer バロメーター 負荷の変化のバロメーターを監視します。
4 Battery バッテリ – バッテリ レベル、ソース、および状態を簡単に検出し
ます。
5 Clipboard クリップボード クリップボード上のテキストをすばやく簡単に設定し
たり読み取ったりします。
6 Color Converters 色の変換機能 System.Drawing.Color のヘルパー メソッド。
7 Compass コンパス 変化のコンパスを監視します。
8 Connectivity 接続 接続状態を確認し、変更を検出します。
9 Detect Shake シェイクの検出 デバイスを振る動きを検出します。
10 Device Display Information デバイス ディスプレイ情報 デバイスの画面のメトリックと向きを取得します。
# 機能名 説明
11 Device Information デバイス情報 デバイスの詳細を簡単に確認します。
12 Email 電子メール 電子メール メッセージを簡単に送信します。
13 File System Helpers ファイル システム ヘルパー アプリ データにファイルを簡単に保存します。
14 Flashlight 懐中電灯 懐中電灯のオン/オフを簡単に切り替える方法です。
15 Geocoding ジオコーディング ジオコードとリバース ジオコードのアドレスおよび座
標。
16 Geolocation 位置情報 デバイスの GPS 位置情報を取得します。
17 Gyroscope ジャイロスコープ デバイスの 3 つの主軸の周りの回転を追跡します。
18 Launcher ランチャー アプリケーションがシステムで URI を開くことができ
るようにします。
19 Magnetometer 磁力計 地球の磁場を基準としたデバイスの向きを検出します。
20 MainThread メイン スレッド アプリケーションのメイン スレッドでコードを実行し
ます。
# 機能名 説明
21 Maps マップ 特定の場所にマップ アプリケーションを開きます。
22 Open Browser ブラウザーを開く ブラウザーで特定の Web サイトをすばやく簡単に開き
ます。
23 Orientation Sensor 向きセンサー 3 次元空間内のデバイスの向きを取得します。
24 Phone Dialer ダイヤラー ダイヤラーを開きます。
25 Platform Extensions プラットフォーム拡張 Rect、Size、Point を変換するためのヘルパー メソッド。
26 Preferences ユーザー設定 永続的なユーザー設定をすばやく簡単に追加します。
27 Secure Storage セキュリティで保護されたストレージ データを安全に格納します。
28 Share 共有 他のアプリにテキストや Web サイトの URI を送信し
ます。
29 SMS SMS 送信用の SMS メッセージを作成します。
30 Text-to-Speech 音声合成 デバイス上のテキストを音声化します。
# 機能名 説明
31 Unit Converters 単位変換機能 単位を変換するためのヘルパー メソッド。
32 Version Tracking バージョンの追跡 アプリケーションのバージョンとビルド番号を追跡し
ます。
33 Vibrate バイブレーション デバイスをバイブレーションさせます。
 Xamarin.Essentials をインストール
 NuGet パッケージをインストール
 Android、iOS、UWP、Xamarin.Forms(.NET Standard) すべてのプロジェクトにイ
ンストールする
 Visual Studio から作成したプロジェクトでは、既定で
インストールされている場合もあります
https://docs.microsoft.com/ja-jp/xamarin/essentials/
 位置情報の例
Platform 名のタブを切り替えて確認
設定内容を確認
https://github.com/xamarin/Essentials
[クラス名].[プラットフォーム名].cs
partial class で宣言
・共通 Interfaceとなるメソッド
・アプリから利用するメソッド
各 Platform
で実装する処理
partial class で宣言
Preference.shared.cs
SharedPreferences
partial class で宣言
Preference.shared.cs
NSUserDefaults
<Project Sdk="MSBuild.Sdk.Extras/2.0.31">
// 中略
</Project>
・Xamarin.iOS, Xamarin.Android の
プラットフォームでもをビルドできるように
OSS の MSBuild 用のライブラリを利用
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT’ ">
netstandard1.0;netstandard2.0;Xamarin.iOS10;Xamarin.TVOS10;Xamarin.W
atchOS10;MonoAndroid60;MonoAndroid70;MonoAndroid71;MonoAndroid80;Mon
oAndroid81;MonoAndroid90;uap10.0.16299;tizen40;
</TargetFrameworks>
ターゲットプラットフォーム
<ItemGroup>
<None Include=“..nugetreadme.txt"
PackagePath="readme.txt" Pack="true" />
<PackageReference Include="mdoc"
Version="5.7.4.3" PrivateAssets="All" />
<PackageReference
Include="System.Numerics.Vectors" Version="4.5.0" />
<Compile Include="***.shared.cs" />
<Compile Include="***.shared.*.cs" />
</ItemGroup>
プラットフォームに関係なく
コンパイルされる
<ItemGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid')) ">
<Compile Include="***.android.cs" />
<Compile Include="***.android.*.cs" />
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors" />
<AndroidResource Include="Resourcesxml*.xml" />
<PackageReference
Include="Xamarin.Android.Support.Compat" Version="28.0.0.1" />
<PackageReference
Include="Xamarin.Android.Support.CustomTabs" Version="28.0.0.1" />
<PackageReference
Include="Xamarin.Android.Support.Core.Utils" Version="28.0.0.1" />
</ItemGroup>
ターゲットが
Xamarin.Android の時
この条件にあう
ファイルをコンパイルする
<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS')) ">
<Compile Include="***.ios.cs" />
<Compile Include="***.ios.*.cs" />
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors" />
<Reference Include="OpenTK-1.0" />
</ItemGroup>
ターゲットが
Xamarin.iOS の時
この条件にあう
ファイルをコンパイルする
これらを参照に含める
public static partial class Preferences
{
public static string Get( … ) =>
PlatformGet<string>(…);
}
Preference.shared.cs
public static partial class Preferences
{
static T PlatformGet<T>(…)
{
/// 中略
}
}
Preferences.android.cs
public static partial class Preferences
{
static T PlatformGet<T>(…)
{
/// 中略
}
}
Preferences.ios.tvos.watchos.cs
Android
iOS
https://docs.microsoft.com/ja-jp/xamarin/essentials/
https://github.com/xamarin/Essentials
2019 08-30 JXUG Xamarin.Essentials #1 Xamarin.Essentials とは
2019 08-30 JXUG Xamarin.Essentials #1 Xamarin.Essentials とは

2019 08-30 JXUG Xamarin.Essentials #1 Xamarin.Essentials とは

  • 4.
  • 5.
     UI コードの共有方法で二通り SharedC# App Logic (.NET Standard) Shared XAML/C# UI Code (Xamarin.Forms) iOS C# UI Shared C# App Logic (.NET Standard) Android C# UI Windows C# UI Xamarin.FormsXamarin Native ロジックのみ共通化 UIはネイティブで個別に作りこむ
  • 6.
     Shared C#App Logic の共通化は? Shared C# App Logic (.NET Standard)
  • 7.
     .NET Standardライブラリのみで実装は不可能  プラットフォーム固有の API を利用する必要がある  Interface を作成後、DependencyService / DI Container 経由で呼び出す Shared C# App Logic (.NET Standard) NSUserDefaults SharedPreferences ApplicationData Interface (C#) UIPasteboard ClipboardManager Clipboard CLLocationManager LocationManager Geolocator アプリ毎に個別実装 していた
  • 8.
     プラットフォーム固有の機能を共通 APIで利用できる ようにした Shared C# App Logic (.NET Standard) NSUserDefaults SharedPreferences ApplicationData Core Cross-Platform API Xamarin.Essentials UIPasteboard ClipboardManager Clipboard CLLocationManager LocationManager Geolocator
  • 9.
     ユーザー設定 NSUserDefaults SharedPreferencesApplicationData Preferences.Set(“KeyName”, 100);
  • 10.
    Accelerometer App InformationBarometer Battery Clipboard Color Converters Compass Connectivity DetectShake Device Display Information Device Information Email File System Helpers Flashlight Geocoding Geolocation Gyroscope Launcher Magnetometer MainThread Maps Open Browser Orientation Sensor Phone Dialer Platform Extensions Preferences Secure Storage Share SMS Text-to-Speech Unit Converters Version Tracking Vibrate
  • 11.
    # 機能名 説明 1Accelerometer 加速度計 3 次元空間内のデバイスの加速度データを取得します。 2 App Information アプリ情報 アプリケーションに関する情報を見つけます。 3 Barometer バロメーター 負荷の変化のバロメーターを監視します。 4 Battery バッテリ – バッテリ レベル、ソース、および状態を簡単に検出し ます。 5 Clipboard クリップボード クリップボード上のテキストをすばやく簡単に設定し たり読み取ったりします。 6 Color Converters 色の変換機能 System.Drawing.Color のヘルパー メソッド。 7 Compass コンパス 変化のコンパスを監視します。 8 Connectivity 接続 接続状態を確認し、変更を検出します。 9 Detect Shake シェイクの検出 デバイスを振る動きを検出します。 10 Device Display Information デバイス ディスプレイ情報 デバイスの画面のメトリックと向きを取得します。
  • 12.
    # 機能名 説明 11Device Information デバイス情報 デバイスの詳細を簡単に確認します。 12 Email 電子メール 電子メール メッセージを簡単に送信します。 13 File System Helpers ファイル システム ヘルパー アプリ データにファイルを簡単に保存します。 14 Flashlight 懐中電灯 懐中電灯のオン/オフを簡単に切り替える方法です。 15 Geocoding ジオコーディング ジオコードとリバース ジオコードのアドレスおよび座 標。 16 Geolocation 位置情報 デバイスの GPS 位置情報を取得します。 17 Gyroscope ジャイロスコープ デバイスの 3 つの主軸の周りの回転を追跡します。 18 Launcher ランチャー アプリケーションがシステムで URI を開くことができ るようにします。 19 Magnetometer 磁力計 地球の磁場を基準としたデバイスの向きを検出します。 20 MainThread メイン スレッド アプリケーションのメイン スレッドでコードを実行し ます。
  • 13.
    # 機能名 説明 21Maps マップ 特定の場所にマップ アプリケーションを開きます。 22 Open Browser ブラウザーを開く ブラウザーで特定の Web サイトをすばやく簡単に開き ます。 23 Orientation Sensor 向きセンサー 3 次元空間内のデバイスの向きを取得します。 24 Phone Dialer ダイヤラー ダイヤラーを開きます。 25 Platform Extensions プラットフォーム拡張 Rect、Size、Point を変換するためのヘルパー メソッド。 26 Preferences ユーザー設定 永続的なユーザー設定をすばやく簡単に追加します。 27 Secure Storage セキュリティで保護されたストレージ データを安全に格納します。 28 Share 共有 他のアプリにテキストや Web サイトの URI を送信し ます。 29 SMS SMS 送信用の SMS メッセージを作成します。 30 Text-to-Speech 音声合成 デバイス上のテキストを音声化します。
  • 14.
    # 機能名 説明 31Unit Converters 単位変換機能 単位を変換するためのヘルパー メソッド。 32 Version Tracking バージョンの追跡 アプリケーションのバージョンとビルド番号を追跡し ます。 33 Vibrate バイブレーション デバイスをバイブレーションさせます。
  • 16.
     Xamarin.Essentials をインストール NuGet パッケージをインストール  Android、iOS、UWP、Xamarin.Forms(.NET Standard) すべてのプロジェクトにイ ンストールする  Visual Studio から作成したプロジェクトでは、既定で インストールされている場合もあります
  • 17.
  • 18.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
    <Project Sdk="MSBuild.Sdk.Extras/2.0.31"> // 中略 </Project> ・Xamarin.iOS,Xamarin.Android の プラットフォームでもをビルドできるように OSS の MSBuild 用のライブラリを利用
  • 30.
    <TargetFrameworks Condition=" '$(OS)'== 'Windows_NT’ "> netstandard1.0;netstandard2.0;Xamarin.iOS10;Xamarin.TVOS10;Xamarin.W atchOS10;MonoAndroid60;MonoAndroid70;MonoAndroid71;MonoAndroid80;Mon oAndroid81;MonoAndroid90;uap10.0.16299;tizen40; </TargetFrameworks> ターゲットプラットフォーム
  • 31.
    <ItemGroup> <None Include=“..nugetreadme.txt" PackagePath="readme.txt" Pack="true"/> <PackageReference Include="mdoc" Version="5.7.4.3" PrivateAssets="All" /> <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" /> <Compile Include="***.shared.cs" /> <Compile Include="***.shared.*.cs" /> </ItemGroup> プラットフォームに関係なく コンパイルされる
  • 32.
    <ItemGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid'))"> <Compile Include="***.android.cs" /> <Compile Include="***.android.*.cs" /> <Reference Include="System.Numerics" /> <Reference Include="System.Numerics.Vectors" /> <AndroidResource Include="Resourcesxml*.xml" /> <PackageReference Include="Xamarin.Android.Support.Compat" Version="28.0.0.1" /> <PackageReference Include="Xamarin.Android.Support.CustomTabs" Version="28.0.0.1" /> <PackageReference Include="Xamarin.Android.Support.Core.Utils" Version="28.0.0.1" /> </ItemGroup> ターゲットが Xamarin.Android の時 この条件にあう ファイルをコンパイルする
  • 33.
    <ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS'))"> <Compile Include="***.ios.cs" /> <Compile Include="***.ios.*.cs" /> <Reference Include="System.Numerics" /> <Reference Include="System.Numerics.Vectors" /> <Reference Include="OpenTK-1.0" /> </ItemGroup> ターゲットが Xamarin.iOS の時 この条件にあう ファイルをコンパイルする これらを参照に含める
  • 34.
    public static partialclass Preferences { public static string Get( … ) => PlatformGet<string>(…); } Preference.shared.cs public static partial class Preferences { static T PlatformGet<T>(…) { /// 中略 } } Preferences.android.cs public static partial class Preferences { static T PlatformGet<T>(…) { /// 中略 } } Preferences.ios.tvos.watchos.cs Android iOS
  • 38.