作成日: 2017/03/20, 更新日: 2017/03/20
メッセンジャー(メッセージボックス)
- (※もし、直接このページにたどり着いた方は、以前投稿した Livetアプリケーション作成時の注意 を読んで、 名前空間やプロジェクト追加に気を付ける必要があることを知ってください)
- 続いてはメッセージボックスを表示してみましょう。
スポンサーリンク
Namespace ViewModels Public Class MessageBoxWindow1ViewModel Inherits ViewModel #Region "Click1Command" Private _Click1Command As ViewModelCommand Public ReadOnly Property Click1Command() As ViewModelCommand Get If _Click1Command Is Nothing Then _Click1Command = New ViewModelCommand(AddressOf Click1) End If Return _Click1Command End Get End Property Private Sub Click1() ' View にメッセージボックスを表示してもらうための依頼 Dim info = New InformationMessage("テストです。", "情報", MessageBoxImage.Information, "Info") Messenger.Raise(info) Console.WriteLine("(もしあれば)後続処理") End Sub #End Region #Region "Click2Command" Private _Click2Command As ViewModelCommand Public ReadOnly Property Click2Command() As ViewModelCommand Get If _Click2Command Is Nothing Then _Click2Command = New ViewModelCommand(AddressOf Click2) End If Return _Click2Command End Get End Property Private Sub Click2() ' View にメッセージボックスを表示してもらうための依頼 Dim confirm = New ConfirmationMessage("今日は、晴れですか?", "確認", MessageBoxImage.Question, MessageBoxButton.YesNo, "Confirm") Messenger.Raise(confirm) ' 結果を受け取って、処理を継続 If confirm.Response.HasValue AndAlso confirm.Response.Value Then Console.WriteLine("「はい」を選択しました、晴れです") Else Console.WriteLine("「いいえ」を選択しました、晴れじゃないです") End If End Sub #End Region #Region "Click3Command" ' View だけで完結した後、何か作業したい時に呼び出されるコールバックコマンド Private _Click3Command As ListenerCommand(Of InformationMessage) Public ReadOnly Property Click3Command() As ListenerCommand(Of InformationMessage) Get If _Click3Command Is Nothing Then _Click3Command = New ListenerCommand(Of InformationMessage)(AddressOf Click3) End If Return _Click3Command End Get End Property Private Sub Click3(ByVal parameter As InformationMessage) Console.WriteLine("(もしあれば)後続処理") End Sub #End Region #Region "Click4Command" ' View だけで完結した後、何か作業したい時に呼び出されるコールバックコマンド Private _Click4Command As ListenerCommand(Of ConfirmationMessage) Public ReadOnly Property Click4Command() As ListenerCommand(Of ConfirmationMessage) Get If _Click4Command Is Nothing Then _Click4Command = New ListenerCommand(Of ConfirmationMessage)(AddressOf Click4) End If Return _Click4Command End Get End Property Private Sub Click4(ByVal parameter As ConfirmationMessage) ' 結果を受け取って、処理を継続 If parameter.Response.HasValue AndAlso parameter.Response.Value Then Console.WriteLine("「はい」を選択しました、晴れです") Else Console.WriteLine("「いいえ」を選択しました、晴れじゃないです") End If End Sub #End Region Public Sub Initialize() End Sub End Class End Namespace
<Window x:Class="Views.MessageBoxWindow1" 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:v="clr-namespace:w10_Messengers.Views" xmlns:vm="clr-namespace:w10_Messengers.ViewModels" Title="MessageBoxWindow1" Height="350" Width="525"> <Window.DataContext> <vm:MessageBoxWindow1ViewModel/> </Window.DataContext> <i:Interaction.Triggers> <!--WindowのContentRenderedイベントのタイミングでViewModelのInitializeメソッドが呼ばれます--> <i:EventTrigger EventName="ContentRendered"> <l:LivetCallMethodAction MethodTarget="{Binding}" MethodName="Initialize"/> </i:EventTrigger> <!--Windowが閉じたタイミングでViewModelのDisposeメソッドが呼ばれます--> <i:EventTrigger EventName="Closed"> <l:DataContextDisposeAction/> </i:EventTrigger> <!-- 情報メッセージを定義、ViewModel からキー名付きで通知が来る、そうしたら情報メッセージを表示する --> <l:InteractionMessageTrigger MessageKey="Info" Messenger="{Binding Messenger}"> <l:InformationDialogInteractionMessageAction /> </l:InteractionMessageTrigger> <!-- 確認メッセージを定義、ViewModel からキー名付きで通知が来る、そうしたら確認メッセージを表示する --> <l:InteractionMessageTrigger MessageKey="Confirm" Messenger="{Binding Messenger}"> <l:ConfirmationDialogInteractionMessageAction /> </l:InteractionMessageTrigger> </i:Interaction.Triggers> <StackPanel Margin="10"> <Button Content="情報メッセージを表示(ViewModel 発信)" Margin="10" Command="{Binding Click1Command}" /> <Button Content="確認メッセージを表示(ViewModel 発信)" Margin="10" Command="{Binding Click2Command}" /> <Button Content="情報メッセージを表示(View 発信)" Margin="10"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <l:InformationDialogInteractionMessageAction> <l:DirectInteractionMessage CallbackCommand="{Binding Click3Command}"> <l:InformationMessage Caption="情報" Text="テストです。" Image="Information" /> </l:DirectInteractionMessage> </l:InformationDialogInteractionMessageAction> </i:EventTrigger> </i:Interaction.Triggers> </Button> <Button Content="確認メッセージを表示(View 発信)" Margin="10"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <l:ConfirmationDialogInteractionMessageAction> <l:DirectInteractionMessage CallbackCommand="{Binding Click4Command}"> <l:ConfirmationMessage Caption="確認" Text="今日は、晴れですか?" Image="Question" Button="YesNo" /> </l:DirectInteractionMessage> </l:ConfirmationDialogInteractionMessageAction> </i:EventTrigger> </i:Interaction.Triggers> </Button> </StackPanel> </Window>
スポンサーリンク



