MahApps.Metro.Controls.Dialogs.MessageDialog.WaitForButtonPressAsync C# (CSharp) 메소드

WaitForButtonPressAsync() 개인적인 메소드

private WaitForButtonPressAsync ( ) : Task
리턴 Task
        internal Task<MessageDialogResult> WaitForButtonPressAsync()
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
                                                       {
                                                           this.Focus();

                                                           var defaultButtonFocus = this.DialogSettings.DefaultButtonFocus;

                                                           //Ensure it's a valid option
                                                           if (!this.IsApplicable(defaultButtonFocus))
                                                           {
                                                               defaultButtonFocus = this.ButtonStyle == MessageDialogStyle.Affirmative
                                                                   ? MessageDialogResult.Affirmative
                                                                   : MessageDialogResult.Negative;
                                                           }

                                                           //kind of acts like a selective 'IsDefault' mechanism.
                                                           switch (defaultButtonFocus)
                                                           {
                                                               case MessageDialogResult.Affirmative:
                                                                   this.PART_AffirmativeButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton");
                                                                   KeyboardNavigationEx.Focus(this.PART_AffirmativeButton);
                                                                   break;
                                                               case MessageDialogResult.Negative:
                                                                   this.PART_NegativeButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton");
                                                                   KeyboardNavigationEx.Focus(this.PART_NegativeButton);
                                                                   break;
                                                               case MessageDialogResult.FirstAuxiliary:
                                                                   this.PART_FirstAuxiliaryButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton");
                                                                   KeyboardNavigationEx.Focus(this.PART_FirstAuxiliaryButton);
                                                                   break;
                                                               case MessageDialogResult.SecondAuxiliary:
                                                                   this.PART_SecondAuxiliaryButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton");
                                                                   KeyboardNavigationEx.Focus(this.PART_SecondAuxiliaryButton);
                                                                   break;
                                                           }
                                                       }));

            TaskCompletionSource<MessageDialogResult> tcs = new TaskCompletionSource<MessageDialogResult>();

            RoutedEventHandler negativeHandler = null;
            KeyEventHandler negativeKeyHandler = null;

            RoutedEventHandler affirmativeHandler = null;
            KeyEventHandler affirmativeKeyHandler = null;

            RoutedEventHandler firstAuxHandler = null;
            KeyEventHandler firstAuxKeyHandler = null;

            RoutedEventHandler secondAuxHandler = null;
            KeyEventHandler secondAuxKeyHandler = null;

            KeyEventHandler escapeKeyHandler = null;

            Action cleanUpHandlers = null;

            var cancellationTokenRegistration = this.DialogSettings.CancellationToken.Register(() =>
                                                                                                   {
                                                                                                       cleanUpHandlers?.Invoke();
                                                                                                       tcs.TrySetResult(this.ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative);
                                                                                                   });

            cleanUpHandlers = () =>
                {
                    this.PART_NegativeButton.Click -= negativeHandler;
                    this.PART_AffirmativeButton.Click -= affirmativeHandler;
                    this.PART_FirstAuxiliaryButton.Click -= firstAuxHandler;
                    this.PART_SecondAuxiliaryButton.Click -= secondAuxHandler;

                    this.PART_NegativeButton.KeyDown -= negativeKeyHandler;
                    this.PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler;
                    this.PART_FirstAuxiliaryButton.KeyDown -= firstAuxKeyHandler;
                    this.PART_SecondAuxiliaryButton.KeyDown -= secondAuxKeyHandler;

                    this.KeyDown -= escapeKeyHandler;

                    cancellationTokenRegistration.Dispose();
                };

            negativeKeyHandler = (sender, e) =>
                {
                    if (e.Key == Key.Enter)
                    {
                        cleanUpHandlers();

                        tcs.TrySetResult(MessageDialogResult.Negative);
                    }
                };

            affirmativeKeyHandler = (sender, e) =>
                {
                    if (e.Key == Key.Enter)
                    {
                        cleanUpHandlers();

                        tcs.TrySetResult(MessageDialogResult.Affirmative);
                    }
                };

            firstAuxKeyHandler = (sender, e) =>
                {
                    if (e.Key == Key.Enter)
                    {
                        cleanUpHandlers();

                        tcs.TrySetResult(MessageDialogResult.FirstAuxiliary);
                    }
                };

            secondAuxKeyHandler = (sender, e) =>
                {
                    if (e.Key == Key.Enter)
                    {
                        cleanUpHandlers();

                        tcs.TrySetResult(MessageDialogResult.SecondAuxiliary);
                    }
                };

            negativeHandler = (sender, e) =>
                {
                    cleanUpHandlers();

                    tcs.TrySetResult(MessageDialogResult.Negative);

                    e.Handled = true;
                };

            affirmativeHandler = (sender, e) =>
                {
                    cleanUpHandlers();

                    tcs.TrySetResult(MessageDialogResult.Affirmative);

                    e.Handled = true;
                };

            firstAuxHandler = (sender, e) =>
                {
                    cleanUpHandlers();

                    tcs.TrySetResult(MessageDialogResult.FirstAuxiliary);

                    e.Handled = true;
                };

            secondAuxHandler = (sender, e) =>
                {
                    cleanUpHandlers();

                    tcs.TrySetResult(MessageDialogResult.SecondAuxiliary);

                    e.Handled = true;
                };

            escapeKeyHandler = (sender, e) =>
                {
                    if (e.Key == Key.Escape)
                    {
                        cleanUpHandlers();

                        tcs.TrySetResult(this.ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative);
                    }
                    else if (e.Key == Key.Enter)
                    {
                        cleanUpHandlers();

                        tcs.TrySetResult(MessageDialogResult.Affirmative);
                    }
                };

            this.PART_NegativeButton.KeyDown += negativeKeyHandler;
            this.PART_AffirmativeButton.KeyDown += affirmativeKeyHandler;
            this.PART_FirstAuxiliaryButton.KeyDown += firstAuxKeyHandler;
            this.PART_SecondAuxiliaryButton.KeyDown += secondAuxKeyHandler;

            this.PART_NegativeButton.Click += negativeHandler;
            this.PART_AffirmativeButton.Click += affirmativeHandler;
            this.PART_FirstAuxiliaryButton.Click += firstAuxHandler;
            this.PART_SecondAuxiliaryButton.Click += secondAuxHandler;

            this.KeyDown += escapeKeyHandler;

            return tcs.Task;
        }

Usage Example

예제 #1
0
        /// <summary>
        /// Creates a MessageDialog ouside of the current window.
        /// </summary>
        /// <param name="window">The MetroWindow</param>
        /// <param name="title">The title of the MessageDialog.</param>
        /// <param name="message">The message contained within the MessageDialog.</param>
        /// <param name="style">The type of buttons to use.</param>
        /// <param name="settings">Optional settings that override the global metro dialog settings.</param>
        /// <returns>A task promising the result of which button was pressed.</returns>
        public static MessageDialogResult ShowModalMessageExternal(this MetroWindow window, string title, string message, MessageDialogStyle style = MessageDialogStyle.Affirmative, MetroDialogSettings settings = null)
        {
            var win = CreateModalExternalWindow(window);

            settings = settings ?? window.MetroDialogOptions;

            //create the dialog control
            var dialog = new MessageDialog(win, settings)
            {
                Message     = message,
                Title       = title,
                ButtonStyle = style
            };

            SetDialogFontSizes(settings, dialog);

            win.Content = dialog;

            MessageDialogResult result = MessageDialogResult.Affirmative;

            dialog.WaitForButtonPressAsync().ContinueWith(task =>
            {
                result = task.Result;
                win.Invoke(win.Close);
            });

            HandleOverlayOnShow(settings, window);
            win.ShowDialog();
            HandleOverlayOnHide(settings, window);
            return(result);
        }
All Usage Examples Of MahApps.Metro.Controls.Dialogs.MessageDialog::WaitForButtonPressAsync