/// <summary> /// Hides a visible dialog. /// </summary> /// <param name="window">The window with the dialog that is visible.</param> /// <param name="dialog">The dialog instance to hide.</param> /// <returns>A task representing the operation.</returns> /// <exception cref="InvalidOperationException"> /// The <paramref name="dialog" /> is not visible in the window. /// This happens if <see cref="ShowDialogAsync" /> hasn't been called before. /// </exception> public static Task HideDialogAsync(this CushWindow window, DialogBase dialog) { window.Dispatcher.VerifyAccess(); var foundDialog = false; var contentDialog = dialog as ContentDialog; if (contentDialog != null) { var dialog2 = contentDialog; foreach (ContentDialog item in window.DialogContainer.Children) { if (item.Guid != dialog2.Guid) continue; foundDialog = true; dialog = item; break; } } else { foundDialog = window.DialogContainer.Children.Contains(dialog); } if (!foundDialog) throw new InvalidOperationException("The provided dialog is not visible in the specified window."); window.SizeChanged -= dialog.SizeChangedHandler; dialog.OnClose(); var closingTask = window.Dispatcher.Invoke(() => dialog.WaitForCloseAsync()); return closingTask.ContinueWith(a => { if (DialogClosed != null) { window.Dispatcher.BeginInvoke( new Action(() => DialogClosed(window, new DialogStateChangedEventArgs()))); } return window.Dispatcher.Invoke(() => { window.DialogContainer.Children.Remove(dialog); //remove the dialog from the container ColorSchemeManager.Release(dialog); return window.HideOverlayAsync(); }); }).Unwrap(); }
private static SizeChangedEventHandler SetupAndOpenDialog(CushWindow window, DialogBase dialog) { dialog.SetValue(Panel.ZIndexProperty, (int) window.OverlayBox.GetValue(Panel.ZIndexProperty) + 1); dialog.MinHeight = window.ActualHeight/4.0; dialog.MaxHeight = window.ActualHeight; //an event handler for auto resizing an open dialog. SizeChangedEventHandler sizeHandler = (sender, args) => { dialog.MinHeight = window.ActualHeight/4.0; dialog.MaxHeight = window.ActualHeight; }; window.SizeChanged += sizeHandler; if (!window.DialogContainer.Children.Contains(dialog)) { window.DialogContainer.Children.Add(dialog); //add the dialog to the container } dialog.OnShown(); return sizeHandler; }