internal ProgressDialogController(ProgressDialog dialog, Func<Task> closeCallBack) { WrappedDialog = dialog; CloseCallback = closeCallBack; IsOpen = dialog.IsVisible; WrappedDialog.PART_NegativeButton.Dispatcher.Invoke(new Action(() => { WrappedDialog.PART_NegativeButton.Click += PART_NegativeButton_Click; })); }
/// <summary> /// Creates a ProgressDialog inside of the current window. /// </summary> /// <param name="title">The title of the ProgressDialog.</param> /// <param name="message">The message within the ProgressDialog.</param> /// <param name="isCancelable">Determines if the cancel button is visible.</param> /// <param name="settings">Optional Settings that override the global metro dialog settings.</param> /// <returns>A task promising the instance of ProgressDialogController for this operation.</returns> public static Task<ProgressDialogController> ShowProgressAsync(this MetroWindow window, string title, string message, bool isCancelable = false, MetroDialogSettings settings = null) { window.Dispatcher.VerifyAccess(); return HandleOverlayOnShow(settings, window).ContinueWith(z => { return ((Task<ProgressDialogController>)window.Dispatcher.Invoke(new Func<Task<ProgressDialogController>>(() => { //create the dialog control ProgressDialog dialog = new ProgressDialog(window); dialog.Message = message; dialog.Title = title; dialog.IsCancelable = isCancelable; if (settings == null) settings = window.MetroDialogOptions; dialog.NegativeButtonText = settings.NegativeButtonText; SizeChangedEventHandler sizeHandler = SetupAndOpenDialog(window, dialog); dialog.SizeChangedHandler = sizeHandler; return dialog.WaitForLoadAsync().ContinueWith(x => { if (DialogOpened != null) { window.Dispatcher.BeginInvoke(new Action(() => DialogOpened(window, new DialogStateChangedEventArgs() { }))); } return new ProgressDialogController(dialog, () => { dialog.OnClose(); if (DialogClosed != null) { window.Dispatcher.BeginInvoke(new Action(() => DialogClosed(window, new DialogStateChangedEventArgs() { }))); } Task closingTask = (Task)window.Dispatcher.Invoke(new Func<Task>(() => dialog._WaitForCloseAsync())); return closingTask.ContinueWith<Task>(a => { return (Task)window.Dispatcher.Invoke(new Func<Task>(() => { window.SizeChanged -= sizeHandler; window.metroDialogContainer.Children.Remove(dialog); //remove the dialog from the container return HandleOverlayOnHide(settings, window); //window.overlayBox.Visibility = System.Windows.Visibility.Hidden; //deactive the overlay effect })); }).Unwrap(); }); }); }))); }).Unwrap(); }