/// <summary> /// Configure <see cref="IDialogWindow"/> and <see cref="IDialogAware"/> events. /// </summary> /// <param name="dialogWindow">The hosting window.</param> /// <param name="callback">The action to perform when the dialog is closed.</param> internal static void ConfigureDialogWindowEvents(this IDialogWindow dialogWindow, Action <IDialogResult> callback) { Action <IDialogResult> requestCloseHandler = null; requestCloseHandler = (o) => { dialogWindow.Result = o; dialogWindow.Close(); }; RoutedEventHandler loadedHandler = null; loadedHandler = (o, e) => { dialogWindow.Loaded -= loadedHandler; dialogWindow.GetDialogViewModel().RequestClose += requestCloseHandler; }; dialogWindow.Loaded += loadedHandler; CancelEventHandler closingHandler = null; closingHandler = (o, e) => { if (!dialogWindow.GetDialogViewModel().CanCloseDialog()) { e.Cancel = true; } }; dialogWindow.Closing += closingHandler; EventHandler closedHandler = null; closedHandler = (o, e) => { dialogWindow.Closed -= closedHandler; dialogWindow.Closing -= closingHandler; dialogWindow.GetDialogViewModel().RequestClose -= requestCloseHandler; dialogWindow.GetDialogViewModel().OnDialogClosed(); if (dialogWindow.Result == null) { dialogWindow.Result = new DialogResult(); } callback?.Invoke(dialogWindow.Result); dialogWindow.DataContext = null; dialogWindow.Content = null; }; dialogWindow.Closed += closedHandler; }
/// <summary> /// Configure <see cref="IDialogWindow"/> and <see cref="IDialogAware"/> events. /// </summary> /// <param name="dialogWindow">The hosting window.</param> /// <param name="callback">The action to perform when the dialog is closed.</param> protected virtual void ConfigureDialogWindowEvents(IDialogWindow dialogWindow, Action <IDialogResult> callback) { void requestCloseHandler(IDialogResult o) { dialogWindow.Result = o; dialogWindow.Close(); } void loadedHandler(object o, RoutedEventArgs e) { dialogWindow.Loaded -= loadedHandler; dialogWindow.GetDialogViewModel().RequestClose += requestCloseHandler; } dialogWindow.Loaded += loadedHandler; void closingHandler(object o, CancelEventArgs e) { if (!dialogWindow.GetDialogViewModel().CanCloseDialog()) { e.Cancel = true; } } dialogWindow.Closing += closingHandler; void closedHandler(object o, EventArgs e) { dialogWindow.Closed -= closedHandler; dialogWindow.Closing -= closingHandler; dialogWindow.GetDialogViewModel().RequestClose -= requestCloseHandler; dialogWindow.GetDialogViewModel().OnDialogClosed(); if (dialogWindow.Result == null) { dialogWindow.Result = new DialogResult(); } callback?.Invoke(dialogWindow.Result); dialogWindow.DataContext = null; dialogWindow.Content = null; } dialogWindow.Closed += closedHandler; }