Visual Studio 2017 でエミュレータ上で Xamarin.Forms をデバッグ実行する
Visual Studio 2017 で、インストール時に「Mobile development with .NET (Preview)」を選択してインストールすれば、Xamarin.Forms のプロジェクトを作れます。
しかし、ただインストールしプロジェクトを作り、そのまま Droid プロジェクトをデバッグ実行しても動きませんでした。
■ Android エミュレータの設定
VS2017 では Android エミュレータとして Google Android エミュレータがインストールされるようです。
このエミュレータがちょっと設定をしないと動きません。
・Hyper-V を停止
Hyper-V が動作していると Google Android エミュレータが動かないので停止します。
次の記事を参考に停止しました。
かずきのBlog@hatena
Hyper-Vを一時的にON/OFF切り替える方法(VMWareと共存させたいときとか)
http://blog.okazuki.jp/entry/2014/04/07/100738
・Intel x86 Atom_64 System Image をインストールする
Android SDK マネージャで「Intel x86 Atom_64 System Image」をインストールします。
( ツール > Android > Android SDK マネージャー )
・Intel x86 Emulator Accelerator をインストールする
Android SKD マネージャで「Intel x86 Emulator Accelerator (HAXM installer)」をインストールします。
「Intel x86 Emulator Accelerator (HAXM installer)」は Extras の中にいます。
この時点では、インストールと言いつつもインストーラがダウンロードされただけの状態です。
ダウンロードされたインストーラを実行し、今度こそ本当にインストールします。
\android-sdk\extras\intel\Hardware_Accelerated_Execution_Manager
の intelhaxm-android.exe を実行します。
・Android Virtual Device を作成する
Android エミュレータ マネージャで Virtual Device を作成します。
( ツール > Android > Android エミュレータ マネージャー )
この際、CPU/ABI で「Intel Atom (x86_64)」を選択してください。
Android エミュレータの設定は次の記事を参考にしました。
Xamarin でも使える高速 Android エミュレータを簡単に作る方法(Windows編)
http://qiita.com/amay077/items/6176fdaf4aa3a13b9a6d
■ Xamarin.Forms プロジェクトの作成
普通にプロジェクトを作成します。
■ RecyclerView のインストール
Xamarin.Forms プロジェクトはすんなり作成できますが、実行しようとすると次のエラーが出ます。
Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly > ‘Xamarin.Android.Support.v7.RecyclerView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=’. Perhaps it doesn’t exist in the Mono for Android profile?
・Xamarin.Android.Support.v7.RecyclerView をインストールする
Droidプロジェクトで NuGet パッケージの管理から Xamarin.Android.Support.v7.RecyclerView をインストールします。
今回はインストール済みの他の「Xamarin.Android.Support.XXXXXXXXXX」にバージョンを合わせて 23.4.0.1 をインストールしました。
インストール時、オプションの「インストールと更新のオプション > 依存関係の動作」を「依存関係を無視する」に設定しインストールします。
■ 実行
リビルドして実行します。
これで、エミュレータ上で実行することができました。
■ おまけ
今回いろいろ試している中で、次のエラーが出ることがありました。
Java.Lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. が発生しました
Java.Lang.IllegalStateException: This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
が発生しました
これらは、必要なNuGet パッケージを整え、リビルドすることで解消しました。