ユニバーサル Windows アプリを作るときに問題になるのは、両方に対応したライブラリじゃないと使えないという点です。PrismAdapterは、ちょっと無理やりにWindowsストアアプリにしか対応していないPrism for WinRTをPhone対応して両方で使えるようにしたもの+VisualStateAwarePageとMvvmAppBaseの継承を不要にしたライブラリです。
簡単な使い方
ユニバーサルWindowsアプリを新規作成して、PrismAdapterをNuGetから入手します。その時の最新バージョンを一応おすすめします(Preだとしても)。今回は0.1.10-beta2をベースに説明します。PrismAdapterを入れたら、NuGetのpackagesフォルダのPrismAdapterのフォルダの中にコードスニペットが入っているのでインストールしておくと捗ります。
Appクラスの編集
AppクラスのOnLaunchedメソッドをPrismAdapter仕様に変更します。普通のコードよりかなりすっきりしてると思います。
using PrismAdapter;
using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
namespace App3
{
public sealed partial class App : Application
{
public App()
{
this.InitializeComponent();
}
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
PrismAdapterBootstrapper.Initialize(b =>
{
b.Run(n => n.Navigate("Main", e.Arguments));
return Task.FromResult(0);
}, e);
}
}
}
ページクラスの作成
ページクラスは、以下のように定型句を入れます。空のページを新規作成してページの属性にPrismを使うこととと、ViewModelを自動で適用する属性をつけます。名前はPrismのデフォルトの規約に従うとViews名前空間に****Pageという形で作成します。MainPage.xamlに属性を追加したコードは以下のようになります。
<Page
xClass="App3.Views.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlnsx="http://schemas.microsoft.com/winfx/2006/xaml"
xmlnslocal="using:App3.Views"
xmlnsd="http://schemas.microsoft.com/expression/blend/2008"
xmlnsmc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlnsprism="using:Microsoft.Practices.Prism.StoreApps"
prismViewModelLocatorAutoWireViewModel="True"
mcIgnorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
</Grid>
</Page>
続けてコードビハインドです。ページを継承しないかわりに、ナビゲーションなどの処理をPrismの仕掛けにのせるために定型句を入れる必要があります。これはpagesetupというコードスニペットで挿入できます。
まず、コードビハインドのページのクラスの中身を空にします。
namespace App3.Views
{
<summary>
</summary>
public sealed partial class MainPage : Page
{
}
}
ページクラスの中にフォーカスをもっていってpagesetup tab tabでコードが展開されます。コンストラクタ名だけ正しいものに編集して、PrismNavigationHelperクラスのためのusing PrismAdapterを追加します。
using PrismAdapter;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace App3.Views
{
<summary>
</summary>
public sealed partial class MainPage : Page
{
public PrismNavigationHelper NavigationHelper { get; set; }
public MainPage()
{
this.InitializeComponent();
this.NavigationHelper = new PrismNavigationHelper(this);
this.NavigationHelper.SaveState += this.NavigationHelper_SaveState;
this.NavigationHelper.LoadState += this.NavigationHelper_LoadState;
this.NavigationHelper.EnablePlatformSupport();
}
private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
}
private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
this.NavigationHelper.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.NavigationHelper.OnNavigatedTo(e);
}
}
}
ViewModelクラスの作成
ViewModelはPrismのデフォルトの命名規約に従うとViewModelsという名前空間に****ViewModelという名前で作成します。ここからはPrismの世界なのでふつうに使っていってください。
まとめ
Prism for WinRTはやくWindows Phone対応してください。