Universal App Platform で Prism を使う

たまには更新したいなと。 既出だったらすいません。

Prism on UAP

Universal App PlatformでPrism for Windows Runtimeを使おうとした時に若干つっかえた点をメモ。

まず、NuGetから落としてくるMicrosoft.Practices.Prism.StoreApps.WindowsではWindowsPhoneで動かそうとすると例外を吐いて動かなくなってしまいます。 なのでソースを落としてきて若干いじります。

Microsoft.Practices.Prism.StoreApps.Windowsのソースは以下にあります。 Microsoft.Practices.Prism.StoreApps.Windows

UAP用のプロジェクト(クラスライブラリ)を新規で作成しこれの一部を書き換えたものを突っ込みます。

Prism.Mvvm.MvvmAppBase for Universal App Platform

突っ込んだらPrism関連のものをNuGetから落として来て入れて、 プロジェクトのリファレンスにWindows Desktop Extension SDKWindows Mobile Extension SDKを入れて完成です。

f:id:cucmberium:20150409203529p:plain

コードのちょっとした解説

そこら中に出回っているのですが、一応。

今までは#ifでそれぞれのデバイスに対してバイナリを吐いていたのを、UAPで一つのバイナリにまとめたため、そのまま使おうとすると例外が出ます。 そのためUAPでは実行時にそのAPIが使用できるかどうかをWindows.Foundation.Metadata.ApiInformationを用いて確認する必要があります。

旧コード

#if WINDOWS_APP
                SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;
#endif

#if WINDOWS_PHONE_APP
                HardwareButtons.BackPressed += OnHardwareButtonsBackPressed;
#endif

新コード

                if (ApiInformation.IsTypePresent("Windows.UI.ApplicationSettings.SettingsPane"))
                    SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;
                if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
                    HardwareButtons.BackPressed += OnHardwareButtonsBackPressed;

既出だったら申し訳ありません。