Livet で Messenger を使ってみる(その弐)

昨日のエントリーのサンプルではつまらないという貴方に、さらに発展させた使い方を考えてみました。
たとえばこんな感じ。コードビハインドを使わずに、コンボボックスで選んだ操作を ViewModel のコマンド経由で実行します。



まず ViewModel のコードです。CommandName プロパティの内容により、起動するメッセージを切り替えます。

#2011/11/10 追記。Livet の作者・尾上さんから InteractionMessageTrigger の MessageKey は同じキーで共有できると Twitter で指摘頂きましたので、2011/11/09 公開時のコードから変更しております。

Public Class MainWindowViewModel
	Inherits ViewModel

#Region "CommandName変更通知プロパティ"
	Private _CommandName As String

	Public Property CommandName() As String
		Get
			Return _CommandName
		End Get
		Set(ByVal value As String)
			If (_CommandName = value) Then Return
			_CommandName = value
			RaisePropertyChanged("CommandName")
		End Set
	End Property
#End Region

#Region "WindowCommand"
	Private _WindowCommand As ViewModelCommand

	Public ReadOnly Property WindowCommand() As ViewModelCommand
		Get
			If _WindowCommand Is Nothing Then
				_WindowCommand = New ViewModelCommand(AddressOf Window)
			End If
			Return _WindowCommand
		End Get
	End Property

	Private Sub Window()
		Select Case Me.CommandName
			' メッセージキーは複数のウィンドウアクションで共有できます。
			Case "閉じる"
				Messenger.Raise(New WindowActionMessage("WindowAction", WindowAction.Close))
			Case "最大化"
				Messenger.Raise(New WindowActionMessage("WindowAction", WindowAction.Maximize))
			Case "最小化"
				Messenger.Raise(New WindowActionMessage("WindowAction", WindowAction.Minimize))
			Case "標準"
				Messenger.Raise(New WindowActionMessage("WindowAction", WindowAction.Normal))
		End Select
	End Sub

#End Region

End Class


次は View。見た目はかなり違いますが、XAML 的には昨日のサンプルを少し改造しただけ。

<Window	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
	xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
	xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
	xmlns:local="clr-namespace:MessengerSample"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
	mc:Ignorable="d" x:Class="MainWindow"
	Title="MainWindow" Height="100" Width="260">
	<i:Interaction.Triggers>
		<l:InteractionMessageTrigger MessageKey="WindowAction" 
			Messenger="{Binding Messenger, Mode=OneWay}">
			<l:WindowInteractionMessageAction/>
		</l:InteractionMessageTrigger>
	</i:Interaction.Triggers>

	<Window.DataContext>
		<local:MainWindowViewModel />
	</Window.DataContext>

	<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="12" />
		<RowDefinition Height="Auto" />
		<RowDefinition />
		<RowDefinition Height="12" />
	</Grid.RowDefinitions>
	<Grid.ColumnDefinitions>
		<ColumnDefinition Width="12" />
		<ColumnDefinition Width="120" />
		<ColumnDefinition Width="12" />
		<ColumnDefinition Width="80" />
		<ColumnDefinition Width="12" />
	</Grid.ColumnDefinitions>
	<ComboBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=CommandName, Mode=OneWayToSource}">
		<ComboBoxItem Content="閉じる" />
		<ComboBoxItem Content="最大化" />
		<ComboBoxItem Content="最小化" />
		<ComboBoxItem Content="標準" />
	</ComboBox>
	<Button Grid.Row="1"  Grid.Column="3"
		Content="実行" 
		Command="{Binding WindowCommand, Mode=OneWay}" />
	</Grid>
</Window>


ぜひ実行して試してみてください。