/// <summary> /// Hides a visible TMP Dialog instance. /// </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="ShowTMPDialogAsync"/> hasn't been called before. /// </exception> public static Task HideTMPDialogAsync(this TMPWindow window, BaseTMPDialog dialog, TMPDialogSettings settings = null) { window.Dispatcher.VerifyAccess(); if (!window.tmpDialogContainer.Children.Contains(dialog)) { throw new InvalidOperationException("The provided dialog is not visible in the specified window."); } window.SizeChanged -= dialog.SizeChangedHandler; dialog.OnClose(); Task closingTask = (Task)window.Dispatcher.Invoke(new Func <Task>(dialog._WaitForCloseAsync)); return(closingTask.ContinueWith(a => { if (DialogClosed != null) { window.Dispatcher.BeginInvoke(new Action(() => DialogClosed(window, new DialogStateChangedEventArgs()))); } return (Task)window.Dispatcher.Invoke(new Func <Task>(() => { window.tmpDialogContainer.Children.Remove(dialog); //remove the dialog from the container return HandleOverlayOnHide(settings, window); })); }).Unwrap()); }
/// <summary> /// Adds a TMP Dialog instance to the specified window and makes it visible. /// <para>Note that this method returns as soon as the dialog is loaded and won't wait on a call of <see cref="HideTMPDialogAsync"/>.</para> /// <para>You can still close the resulting dialog with <see cref="HideTMPDialogAsync"/>.</para> /// </summary> /// <param name="window">The owning window of the dialog.</param> /// <param name="dialog">The dialog instance itself.</param> /// <returns>A task representing the operation.</returns> /// <exception cref="InvalidOperationException">The <paramref name="dialog"/> is already visible in the window.</exception> public static Task ShowTMPDialogAsync(this TMPWindow window, BaseTMPDialog dialog, TMPDialogSettings settings = null) { window.Dispatcher.VerifyAccess(); if (window.tmpDialogContainer.Children.Contains(dialog)) { throw new InvalidOperationException("The provided dialog is already visible in the specified window."); } return(HandleOverlayOnShow(settings, window).ContinueWith(z => { dialog.Dispatcher.Invoke(new Action(() => { SizeChangedEventHandler sizeHandler = SetupAndOpenDialog(window, dialog); dialog.SizeChangedHandler = sizeHandler; })); }).ContinueWith(y => ((Task)dialog.Dispatcher.Invoke(new Func <Task>(() => dialog.WaitForLoadAsync().ContinueWith(x => { dialog.OnShown(); if (DialogOpened != null) { DialogOpened(window, new DialogStateChangedEventArgs()); } })))))); }
public static BaseTMPDialog ShowModalDialogExternally(this BaseTMPDialog dialog) { Window win = SetupExternalDialogWindow(dialog); dialog.OnShown(); win.ShowDialog(); return(dialog); }
private static Window SetupExternalDialogWindow(BaseTMPDialog dialog) { var win = new TMPWindow { ShowInTaskbar = false, ShowActivated = true, Topmost = true, ResizeMode = ResizeMode.NoResize, WindowStyle = WindowStyle.None, WindowStartupLocation = WindowStartupLocation.CenterScreen, ShowTitleBar = false, ShowCloseButton = false, Background = dialog.Background }; try { win.GlowBrush = win.TryFindResource("AccentColorBrush") as SolidColorBrush; } catch (Exception) { } win.Width = SystemParameters.PrimaryScreenWidth; win.MinHeight = SystemParameters.PrimaryScreenHeight / 4.0; win.SizeToContent = SizeToContent.Height; var glowWindow = new GlowWindowBehavior(); glowWindow.Attach(win); dialog.ParentDialogWindow = win; //THIS IS ONLY, I REPEAT, ONLY SET FOR EXTERNAL DIALOGS! win.Content = dialog; EventHandler closedHandler = null; closedHandler = (sender, args) => { win.Closed -= closedHandler; dialog.ParentDialogWindow = null; win.Content = null; }; win.Closed += closedHandler; return(win); }
private static SizeChangedEventHandler SetupAndOpenDialog(TMPWindow window, BaseTMPDialog dialog) { dialog.SetValue(Panel.ZIndexProperty, (int)window.overlayBox.GetValue(Panel.ZIndexProperty) + 1); dialog.MinHeight = window.ActualHeight / 4.0; dialog.MaxHeight = window.ActualHeight; SizeChangedEventHandler sizeHandler = (sender, args) => { dialog.MinHeight = window.ActualHeight / 4.0; dialog.MaxHeight = window.ActualHeight; }; window.SizeChanged += sizeHandler; window.tmpDialogContainer.Children.Add(dialog); //add the dialog to the container dialog.OnShown(); return(sizeHandler); }