かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

Windows store appとWindows Phone appでPrism for WinRTを使う

ユニバーサル 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
    x:Class="App3.Views.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App3.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
    prism:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="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;

// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=234238 を参照してください

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対応してください。