前回から少し時間が空きましたが、Prism+DryIoc入門の第3弾です。一応これで一区切りのつもりです。
前回はPrism+DryIocでプロジェクトを立ち上げるお作法を紹介しました。これに加えてWPFの基礎知識がある皆さんはそれなりのソフトを作ることができると思います。ですが、ひとつ足りないことがありました。それはダイアログの表示です。今回はそのダイアログの表示の方法について説明していきます。
ダイアログの実装
さて、通常、ダイアログを開くとなるとウィンドウを作ることになりますが、PrismではUserControlを作ることになります。ちょうどウィンドウ内でRegionを作るのと同じような感じですね。
順に説明していきます。
1. Viewの作成
「追加」→「新しい項目」からPrism UserControl (WPF)を追加します。
出来上がったXAMLファイルに前回の記事に書いたとおりのお作法でIgnorebleなどを設定します。
<UserControl x:Class="PrismTest.Views.TestDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:PrismTest.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vm:TestDialogViewModel}"
prism:ViewModelLocator.AutoWireViewModel="True"
Height="350" Width="525" >
<Grid />
</UserControl>
2. VideModelの作成
ViewModelにはIDialogAwareを実装する必要があります。これはPrismにこれはダイアログですよと教えるためのものですね。
public class TestDialogViewModel : BindableBase, IDialogAware
{
public TestDialogViewModel()
{
}
public string Title => "Test Dialog";
public event Action<IDialogResult>? RequestClose;
public bool CanCloseDialog() => true;
public void OnDialogClosed()
{
}
public void OnDialogOpened(IDialogParameters parameters)
{
}
}
タイトルやいくつかのメソッド、ダイアログを閉じる際に呼ぶイベントなどを実装します。特段説明をしなくても、その名前から何のためのものかはすぐにわかると思います。
3. Viewをダイアログとして登録
App.xaml.csを開き、RegisterTypesメソッド中にて作った型をダイアログとして登録します。これを登録することで、PrismがこのViewがダイアログであることを認識してくれます。
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterDialog<TestDialog>("TestDialog");
}
}
これも前回の記事までを読んだ人なら特に困ることは無いと思います。
4. ダイアログを表示
さて、ダイアログの表示も至って簡単です。ライブラリ無しではMVVMのポリシーを保ちつつViewModelから別ウィンドウを表示するのがとても面倒だったので、さすがPrismという感じですね。
public class ContentRegionViewModel : BindableBase
{
public ContentRegionViewModel(IDialogService dialog)
{
ShowDialogCommand = new DelegateCommand(() => dialog.ShowDialog("TestDialog"));
}
public DelegateCommand ShowDialogCommand { get; }
}
適当なViewModelでIDialogServiceを受け取るようにすると、DryIoc経由でダイアログ関係の機能にアクセスできるようになります。ダイアログを表示するにはShowDialogメソッドを呼ぶだけです。引数はApp.xaml.csで設定した名前ですね。
このコマンドを呼び出すと、無事ダイアログが表示されました。
ダイアログを作りこむ
1. ウィンドウスタイル
さて、ダイアログが表示されましたが、よく見ると最大化/最小化ボタンなどが表示されていますね。Windows11のデザインではわかりにくいですが、ウィンドウもリサイズ可能なものになっています。さらにタスクバーを見るとダイアログが単体のアイコンを持っています。イケてないですね。
端的に、普通のウィンドウが表示されたと言えます。ダイアログはもっとダイアログっぽいスタイルでウィンドウを表示したいですね。
これに関しては、UserControlにprism:Dialog.WindowStyle添付プロパティを設定してやれば良いです。
<UserControl x:Class="PrismTest.Views.TestDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:PrismTest.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vm:TestDialogViewModel}"
prism:ViewModelLocator.AutoWireViewModel="True"
Height="350" Width="525" >
<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="prism:Dialog.WindowStartupLocation" Value="CenterOwner" />
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="ShowInTaskbar" Value="False"/>
<Setter Property="SizeToContent" Value="WidthAndHeight"/>
</Style>
</prism:Dialog.WindowStyle>
<Grid />
</UserControl>
Windows11のデザインではわかりにくいですが、ウィンドウ枠が細枠(サイズ変更不可)になっており、最小化/最大化のボタンもなくなっています。もちろんタスクバーにも表示はされていません。
2. データの受け渡しをする
さて、ダイアログを表示するときは何かしらのデータを引き渡したいし、ダイアログを閉じた時には入力されたデータを受け取りたいことがあります。安心してください、Prismではそのような機能もサポートしています。
public class ContentRegionViewModel : BindableBase
{
public ContentRegionViewModel(IDialogService dialog)
{
ShowDialogCommand = new DelegateCommand(() => {
var param = new DialogParameters() { { "Text", Text }, };
dialog.ShowDialog("TestDialog", param, result => {
if(result.Result == ButtonResult.OK)
Text = result.Parameters.GetValue<string>("Text");
});
});
}
public DelegateCommand ShowDialogCommand { get; }
public string Text
{
get => _Text;
set => SetProperty(ref _Text, value);
}
string _Text = "";
}
呼び出し側のViewModelはこんな感じです。ShowDialogの第2引数にDialogParametersを渡します。名前こそDialogParametersですが、単純なディクショナリ型ですので、任意のデータを入れ込むことができます。
第3引数はダイアログから制御が返ってきたときに呼び出されるデリゲートです。下のダイアログ側のViewModelを見てからのほうがわかりやすいと思いますので、ひとまずそちらを見てから解説します。
public class TestDialogViewModel : BindableBase, IDialogAware
{
public TestDialogViewModel()
{
OkButtonCommand = new DelegateCommand(() => RaiseRequestClose(ButtonResult.OK, new DialogParameters() { { "Text", Text } }));
CancelButtonCommand = new DelegateCommand(() => RaiseRequestClose(ButtonResult.Cancel));
}
public string Title => "Test Dialog";
public void OnDialogOpened(IDialogParameters parameters)
{
Text = parameters.GetValue<string>("Text");
}
public void OnDialogClosed()
{
}
public bool CanCloseDialog() => true;
protected void RaiseRequestClose(ButtonResult btnres, IDialogParameters param)
{
RequestClose?.Invoke(new DialogResult(btnres, param));
}
protected void RaiseRequestClose(ButtonResult btnres)
{
RequestClose?.Invoke(new DialogResult(btnres));
}
public event Action<IDialogResult>? RequestClose;
public DelegateCommand OkButtonCommand { get; }
public DelegateCommand CancelButtonCommand { get; }
public string Text
{
get => _Text;
set => SetProperty(ref _Text, value);
}
string _Text = "";
}

OnDialogOpenedメソッドはIDialogAwareインターフェースのメンバーの一つですが、ダイアログが開いたときにPrismがこのメソッドを呼んでくれます。この引数が、呼び出し元がパラメーターとして指定したものになります。今回は呼び出し物が指定したTextを読み取って、Textプロパティに代入しています。
OKボタン/Cancelボタンが押されたときにそれぞれRequestCloseイベントを発動させています。その引数にはDialogResultクラスで、どのボタンが押されたかとDialogParametersをコンストラクタに渡してあげることで、その値を呼び出し元に返すことができます。
そして、呼び出し元のコードでは、OKボタンが押されたことを受けて、ShowDialogメソッドの第3引数のデリゲートが呼ばれますので、そこでButtonResultやDialogParametersを読み取って、適宜何かしらの処理を追加してやれば良いです。
ちなみに、ShowDialogメソッドは同期的に呼ばれます。すなわち、第3引数のデリゲートが制御を返した後にShowDialogメソッドは制御を返します。じゃあなんでShowDialogメソッドの戻り値をIDialogResultにしてくれなかったんだ…。
番外編:標準ダイアログを表示する
さて、ダイアログの表示のしかたは分かったけど、MessageBoxやファイルを開くダイアログなどのWindows標準ダイアログを表示させるにはどうしたら良いでしょう?
結論から言うと、Prismではサポートしていません。今まで説明してきた通り、ダイアログを実装するための支援が充実しているから、MessageBoxやファイルを開くダイアログは自作してねということのようです。
確かに、最近そういったダイアログも独自実装されたアプリをしばしば見かけますが、個人的にはソウジャナイ感をどうしても感じてしまいます。そういう標準ダイアログは、アプリを作る側の省力化だけでなく、使う側としてもどのアプリを使っても同じデザインだからOSとして統一感があり使いやすいわけです。それをわざわざ「自分で実装しろ」というのはいかがなものか…。
というわけで、標準ダイアログを表示するためにはLivetを使いましょう。大丈夫、PrismとLivetも共存できます。
まずは NugetからLivet.Messagingを入れます。
public class ContentRegionViewModel : BindableBase
{
public ContentRegionViewModel()
{
Messenger = new InteractionMessenger();
ShowMessageBoxCommand = new DelegateCommand(() => Messenger.Raise(new InformationMessage("MessageBox Text", "Caption", System.Windows.MessageBoxImage.Information, "InformationMessageBox")));
}
public InteractionMessenger Messenger { get; }
public DelegateCommand ShowMessageBoxCommand { get; }
}
その後、ViewModelにInteractionMessengerクラスのプロパティを一つ用意してあげます。Livet標準のViewModel基底型ならもとからこのプロパティを持っているのですが、当然ながらPrismには無いので後付けしてやる必要があります。
<UserControl x:Class="PrismTest.Views.ContentRegion"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
xmlns:vm="clr-namespace:PrismTest.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vm:ContentRegionViewModel}"
prism:ViewModelLocator.AutoWireViewModel="True"
d:Height="350" d:Width="525" >
<i:Interaction.Triggers>
<l:InteractionMessageTrigger Messenger="{Binding Messenger}" MessageKey="InformationMessageBox">
<l:InformationDialogInteractionMessageAction InvokeActionOnlyWhenWindowIsActive="False" />
</l:InteractionMessageTrigger>
</i:Interaction.Triggers>
<Grid>
<Button Content="Show MessageBox" Command="{Binding ShowMessageBoxCommand}" Width="150" Height="40" />
</Grid>
</UserControl>
ViewではInteractionMessageTriggerを使い、ViewModelからのメッセージを受け取ってやれば良いです。今回はOKボタンのみのMessageBoxを表示させていますが、その他の種類のMessageBoxだったりファイルを開くダイアログだったりは、対応する***Message型/***InteractionMessageAction型に差し替えれば表示することができます。ここらへんはLivetの使い方ですので、だいぶ昔に書いたこちらの記事を見ていただければ良いかと思います。
Prismの入門記事でPrismではなく別のライブラリを使えというのも変な話ですが、ライブラリによって当然得手不得手というものはありますから、それによって組み合わせが発生するのはやむを得ないことと思います。
***
以上で、3回にわたって連載(?)してきたPrism+DryIocの入門編ですが、ひとまず今回で終わりにしたいと思います。また何か思いついたテーマがあれば随時記事にします。