コード例 #1
0
        /// <summary>
        /// Windowクラスに対する共通初期設定
        ///  Windowクラスのコンストラクタ内で、InitializeComponentより前に呼び出す。
        /// </summary>
        /// <param name="window">this : Window</param>
        /// <param name="options">初期設定オプションのセット : int</param>
        public static void Setup(this Window window, WindowSetupOptions options = WindowSetupOptions.SetAll)
        {
            // DataContext変更時のイベントハンドラを設定する
            window.DataContextChanged += (_, __) =>
            {
                var vm = window.DataContext as ViewModelBase;

                if (vm != null)
                {
                    // ShowMessageBoxイベントのハンドラを設定
                    if ((options & WindowSetupOptions.SetShowMessageBoxHandler) == WindowSetupOptions.SetShowMessageBoxHandler)
                    {
                        vm.ShowMessageBox += (sender, arg) =>
                        {
                            arg.Result = MessageBox.Show(arg.Message, arg.Caption, arg.Buttons, arg.Icon);
                        };
                    }

                    // CloseViewイベントのハンドラを設定
                    if ((options & WindowSetupOptions.SetCloseViewHandler) == WindowSetupOptions.SetCloseViewHandler)
                    {
                        vm.CloseView += (sender, arg) =>
                        {
                            // モーダルダイアログならDialogResultを設定、モードレスなら Closeメソッドを呼ぶ
                            if (ComponentDispatcher.IsThreadModal)
                            {
                                window.DialogResult = arg.DialogResult;
                            }
                            else
                            {
                                window.Close();
                            }
                        };
                    }
                }
            };

            // ClosingイベントでViewModelのCanCloseView()をコールする
            window.Closing += (_, e) =>
            {
                var vm = window.DataContext as ViewModelBase;
                if (vm != null)
                {
                    e.Cancel = !vm.CanCloseView(new ViewModelBase.CanCloseViewArgs(ComponentDispatcher.IsThreadModal, window.DialogResult));
                }
            };

            // ClosedイベントでViewModelのClosedView()をコールする
            window.Closed += (_, __) =>
            {
                var vm = window.DataContext as ViewModelBase;
                if (vm != null)
                {
                    vm.ClosedView(new ViewModelBase.ClosedViewArgs(ComponentDispatcher.IsThreadModal, window.DialogResult));
                }
            };
        }
コード例 #2
0
        /// <summary>
        /// ContentControlクラスに対する共通初期設定
        ///  ContentControlクラスのコンストラクタ内で、InitializeComponentより前に呼び出す。
        /// </summary>
        /// <param name="control">this : ContentControl</param>
        /// <param name="options">初期設定オプションのセット : int</param>
        public static void Setup(this ContentControl control, WindowSetupOptions options = WindowSetupOptions.SetAll)
        {
            // DataContext変更時のイベントハンドラを設定する
            control.DataContextChanged += (o, e) =>
            {
                // 以前のViewModelに切断通知を送る
                if (e.OldValue is ViewModelBase)
                {
                    var oldViewMolde = e.OldValue as ViewModelBase;
                    oldViewMolde?.DisconnectedView(control);
                }

                var vm = control.DataContext as ViewModelBase;
                if (vm != null)
                {
                    // GetViewデレゲートの設定
                    vm.GetView = () => { return(control); };

                    // ShowMessageBoxイベントのハンドラを設定
                    if ((options & WindowSetupOptions.SetShowMessageBoxHandler) == WindowSetupOptions.SetShowMessageBoxHandler)
                    {
                        vm.ShowMessageBox += (sender, arg) =>
                        {
                            arg.Result = MessageBox.Show(arg.Message, arg.Caption, arg.Buttons, arg.Icon);
                        };
                    }

                    // CloseViewイベントのハンドラを設定
                    if ((options & WindowSetupOptions.SetCloseViewHandler) == WindowSetupOptions.SetCloseViewHandler)
                    {
                        vm.CloseView += (sender, arg) =>
                        {
                            if (control is Window)
                            {
                                // モーダルダイアログならDialogResultを設定、モードレスなら Closeメソッドを呼ぶ
                                if (ComponentDispatcher.IsThreadModal)
                                {
                                    (control as Window).DialogResult = arg.DialogResult;
                                }
                                else
                                {
                                    (control as Window).Close();
                                }
                            }
                        };
                    }

                    // ViewModelへ接続を通知する
                    vm.ConnectedView(control);
                }
            };

            if (control is Window)
            {
                var window = control as Window;
                // ClosingイベントでViewModelのCanCloseViewe()をコールする
                window.Closing += (o, e) =>
                {
                    var vm = control.DataContext as ViewModelBase;
                    if (vm != null)
                    {
                        e.Cancel = !vm.CanCloseView(new ViewModelBase.CanCloseViewArgs(ComponentDispatcher.IsThreadModal, window.DialogResult));
                    }
                };

                // ClosedイベントでViewModelのClosedView()をコールする
                window.Closed += (o, e) =>
                {
                    var vm = control.DataContext as ViewModelBase;
                    if (vm != null)
                    {
                        vm.ClosedView(new ViewModelBase.ClosedViewArgs(ComponentDispatcher.IsThreadModal, window.DialogResult));
                    }
                };
            }
        }