private void MessageReceived(object sender, [NotNull] InteractionMessageRaisedEventArgs e) { if (e == null) { throw new ArgumentNullException(nameof(e)); } var message = e.Message; var cloneMessage = (InteractionMessage)message.Clone(); cloneMessage.Freeze(); var checkResult = false; void CheckAction() { if (!IsEnable) { return; } if (InvokeActionsOnlyWhileAttatchedObjectLoaded && !_loaded) { return; } if (!(string.IsNullOrEmpty(MessageKey) || MessageKey == cloneMessage.MessageKey)) { return; } checkResult = true; } DoActionOnDispatcher(CheckAction); if (!checkResult) { return; } DoActionOnDispatcher(() => InvokeActions(cloneMessage)); var responsiveMessage = message as ResponsiveInteractionMessage; object response; if (responsiveMessage != null && (response = ((ResponsiveInteractionMessage)cloneMessage).Response) != null) { responsiveMessage.Response = response; } }
/// <summary> /// Messenger の Raised イベント処理です。メッセージキーに応じた各処理をおこないます。 /// </summary> /// <param name="window">Window</param> /// <param name="sender">Messenger</param> /// <param name="e">InteractionMessageRaisedEventArgs</param> private static void Messenger_Raised(Window window, object sender, InteractionMessageRaisedEventArgs e) { // 既存の XxxTrigger や XxxAction にお任せ案 (View に書く版のクラスの再利用と処理のお任せ) は、難しいため諦めた。 //var mes = new InformationDialogInteractionMessageAction(); // 非同期で呼ばれた場合は、同期呼び出し(UI スレッド)に変えて実行する if (!window.Dispatcher.CheckAccess()) { var recall = new Action <Window, object, InteractionMessageRaisedEventArgs>((w, s, e2) => Messenger_Raised(w, s, e2)); window.Dispatcher.Invoke(recall, new object[] { window, sender, e }); return; } switch (e.Message.MessageKey) { case "ShowInformationMessage": case "ShowWarningMessage": case "ShowErrorMessage": case "ShowInformationMessageAsync": case "ShowWarningMessageAsync": case "ShowErrorMessageAsync": // 表示のみの MessageBox 系 var informationMessage = e.Message as InformationMessage; if (informationMessage != null) { MessageBox.Show( window, informationMessage.Text, informationMessage.Caption, MessageBoxButton.OK, informationMessage.Image ); } break; case "ShowConfirmationOKCancelMessage": case "ShowConfirmationYesNoMessage": case "ShowConfirmationYesNoCancelMessage": case "ShowConfirmationOKCancelMessageAsync": case "ShowConfirmationYesNoMessageAsync": case "ShowConfirmationYesNoCancelMessageAsync": // 戻り値ありの MessageBox 系 var confirmMessage = e.Message as ConfirmationMessage; if (confirmMessage != null) { var result = MessageBox.Show( window, confirmMessage.Text, confirmMessage.Caption, confirmMessage.Button, confirmMessage.Image, confirmMessage.DefaultResult ); if (result == MessageBoxResult.Yes || result == MessageBoxResult.OK) { confirmMessage.Response = true; } else if (result == MessageBoxResult.Cancel) { confirmMessage.Response = null; } else { confirmMessage.Response = false; } } break; case "Close": case "CloseAsync": var closeMessage = e.Message as WindowActionMessage; if (closeMessage != null && closeMessage.Action == WindowAction.Close) { window.Close(); } break; case "Maximize": case "MaximizeAsync": var maximizeMessage = e.Message as WindowActionMessage; if (maximizeMessage != null && maximizeMessage.Action == WindowAction.Maximize) { window.WindowState = WindowState.Maximized; } break; case "Minimize": case "MinimizeAsync": var minimizeMessage = e.Message as WindowActionMessage; if (minimizeMessage != null && minimizeMessage.Action == WindowAction.Minimize) { window.WindowState = WindowState.Minimized; } break; case "Active": case "ActiveAsync": var activeMessage = e.Message as WindowActionMessage; if (activeMessage != null && activeMessage.Action == WindowAction.Active) { window.Activate(); } break; } }