internal void SetDefaultButton() { this.Focus(); //kind of acts like a selective 'IsDefault' mechanism. switch (this.ButtonStyle) { case MessageDialogStyle.Affirmative: PART_AffirmativeButton.Focus(); break; case MessageDialogStyle.AffirmativeAndNegative: case MessageDialogStyle.AffirmativeAndNegativeAndDoubleAuxiliary: case MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary: PART_NegativeButton.Focus(); break; } }
internal Task <MessageDialogResult> WaitForButtonPressAsync() { Dispatcher.BeginInvoke(new Action(() => { this.Focus(); //kind of acts like a selective 'IsDefault' mechanism. switch (this.ButtonStyle) { case MessageDialogStyle.Affirmative: PART_AffirmativeButton.Focus(); break; case MessageDialogStyle.AffirmativeAndNegative: case MessageDialogStyle.AffirmativeAndNegativeAndDoubleAuxiliary: case MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary: PART_NegativeButton.Focus(); 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 = DialogSettings.CancellationToken.Register(() => { cleanUpHandlers(); tcs.TrySetResult(ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative); }); cleanUpHandlers = () => { PART_NegativeButton.Click -= negativeHandler; PART_AffirmativeButton.Click -= affirmativeHandler; PART_FirstAuxiliaryButton.Click -= firstAuxHandler; PART_SecondAuxiliaryButton.Click -= secondAuxHandler; PART_NegativeButton.KeyDown -= negativeKeyHandler; PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler; PART_FirstAuxiliaryButton.KeyDown -= firstAuxKeyHandler; PART_SecondAuxiliaryButton.KeyDown -= secondAuxKeyHandler; 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(ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative); } else if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Affirmative); } }; PART_NegativeButton.KeyDown += negativeKeyHandler; PART_AffirmativeButton.KeyDown += affirmativeKeyHandler; PART_FirstAuxiliaryButton.KeyDown += firstAuxKeyHandler; PART_SecondAuxiliaryButton.KeyDown += secondAuxKeyHandler; PART_NegativeButton.Click += negativeHandler; PART_AffirmativeButton.Click += affirmativeHandler; PART_FirstAuxiliaryButton.Click += firstAuxHandler; PART_SecondAuxiliaryButton.Click += secondAuxHandler; KeyDown += escapeKeyHandler; return(tcs.Task); }
internal Task <MessageDialogResult> WaitForButtonPressAsync() { Dispatcher.BeginInvoke(new Action(() => { this.Focus(); //kind of acts like a selective 'IsDefault' mechanism. if (ButtonStyle == MessageDialogStyle.Affirmative) { PART_AffirmativeButton.Focus(); } else if (ButtonStyle == MessageDialogStyle.AffirmativeAndNegative) { PART_NegativeButton.Focus(); } })); 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; Action cleanUpHandlers = () => { PART_NegativeButton.Click -= negativeHandler; PART_AffirmativeButton.Click -= affirmativeHandler; PART_FirstAuxiliaryButton.Click -= firstAuxHandler; PART_SecondAuxiliaryButton.Click -= secondAuxHandler; PART_NegativeButton.KeyDown -= negativeKeyHandler; PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler; PART_FirstAuxiliaryButton.KeyDown -= firstAuxKeyHandler; PART_SecondAuxiliaryButton.KeyDown -= secondAuxKeyHandler; }; negativeKeyHandler = new KeyEventHandler((sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Negative); } }); affirmativeKeyHandler = new KeyEventHandler((sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Affirmative); } }); firstAuxKeyHandler = new KeyEventHandler((sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.FirstAuxiliary); } }); secondAuxKeyHandler = new KeyEventHandler((sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.SecondAuxiliary); } }); negativeHandler = new RoutedEventHandler((sender, e) => { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Negative); e.Handled = true; }); affirmativeHandler = new RoutedEventHandler((sender, e) => { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Affirmative); e.Handled = true; }); firstAuxHandler = new RoutedEventHandler((sender, e) => { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.FirstAuxiliary); e.Handled = true; }); secondAuxHandler = new RoutedEventHandler((sender, e) => { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.SecondAuxiliary); e.Handled = true; }); PART_NegativeButton.KeyDown += negativeKeyHandler; PART_AffirmativeButton.KeyDown += affirmativeKeyHandler; PART_FirstAuxiliaryButton.KeyDown += firstAuxKeyHandler; PART_SecondAuxiliaryButton.KeyDown += secondAuxKeyHandler; PART_NegativeButton.Click += negativeHandler; PART_AffirmativeButton.Click += affirmativeHandler; PART_FirstAuxiliaryButton.Click += firstAuxHandler; PART_SecondAuxiliaryButton.Click += secondAuxHandler; return(tcs.Task); }
internal Task <MessageDialogResult> WaitForButtonPressAsync() { Dispatcher.BeginInvoke(new Action(() => { Focus(); var defaultButtonFocus = DialogSettings.DefaultButtonFocus; //Ensure it's a valid option if (!IsApplicable(defaultButtonFocus)) { defaultButtonFocus = ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative; } //kind of acts like a selective 'IsDefault' mechanism. switch (defaultButtonFocus) { case MessageDialogResult.Affirmative: PART_AffirmativeButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton"); KeyboardNavigationEx.Focus(PART_AffirmativeButton); break; case MessageDialogResult.Negative: PART_NegativeButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton"); KeyboardNavigationEx.Focus(PART_NegativeButton); break; case MessageDialogResult.FirstAuxiliary: PART_FirstAuxiliaryButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton"); KeyboardNavigationEx.Focus(PART_FirstAuxiliaryButton); break; case MessageDialogResult.SecondAuxiliary: PART_SecondAuxiliaryButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton"); KeyboardNavigationEx.Focus(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 = DialogSettings.CancellationToken.Register(async() => { var result = ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative; if (await _buttonPressCallbackAsync(result)) { cleanUpHandlers?.Invoke(); tcs.TrySetResult(result); } }); cleanUpHandlers = () => { PART_NegativeButton.Click -= negativeHandler; PART_AffirmativeButton.Click -= affirmativeHandler; PART_FirstAuxiliaryButton.Click -= firstAuxHandler; PART_SecondAuxiliaryButton.Click -= secondAuxHandler; PART_NegativeButton.KeyDown -= negativeKeyHandler; PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler; PART_FirstAuxiliaryButton.KeyDown -= firstAuxKeyHandler; PART_SecondAuxiliaryButton.KeyDown -= secondAuxKeyHandler; KeyDown -= escapeKeyHandler; cancellationTokenRegistration.Dispose(); }; negativeKeyHandler = async(sender, e) => { if (e.Key == Key.Enter) { if (await _buttonPressCallbackAsync(MessageDialogResult.Negative)) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Negative); } } }; affirmativeKeyHandler = async(sender, e) => { if (e.Key == Key.Enter) { if (await _buttonPressCallbackAsync(MessageDialogResult.Affirmative)) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Affirmative); } } }; firstAuxKeyHandler = async(sender, e) => { if (e.Key == Key.Enter) { if (await _buttonPressCallbackAsync(MessageDialogResult.FirstAuxiliary)) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.FirstAuxiliary); } } }; secondAuxKeyHandler = async(sender, e) => { if (e.Key == Key.Enter) { if (await _buttonPressCallbackAsync(MessageDialogResult.SecondAuxiliary)) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.SecondAuxiliary); } } }; negativeHandler = async(sender, e) => { if (await _buttonPressCallbackAsync(MessageDialogResult.Negative)) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Negative); e.Handled = true; } }; affirmativeHandler = async(sender, e) => { if (await _buttonPressCallbackAsync(MessageDialogResult.Affirmative)) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Affirmative); e.Handled = true; } }; firstAuxHandler = async(sender, e) => { if (await _buttonPressCallbackAsync(MessageDialogResult.FirstAuxiliary)) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.FirstAuxiliary); e.Handled = true; } }; secondAuxHandler = async(sender, e) => { if (await _buttonPressCallbackAsync(MessageDialogResult.SecondAuxiliary)) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.SecondAuxiliary); e.Handled = true; } }; escapeKeyHandler = async(sender, e) => { if (e.Key == Key.Escape) { var result = ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative; if (await _buttonPressCallbackAsync(result)) { cleanUpHandlers(); tcs.TrySetResult(result); } } else if (e.Key == Key.Enter) { if (await _buttonPressCallbackAsync(MessageDialogResult.Affirmative)) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Affirmative); } } }; PART_NegativeButton.KeyDown += negativeKeyHandler; PART_AffirmativeButton.KeyDown += affirmativeKeyHandler; PART_FirstAuxiliaryButton.KeyDown += firstAuxKeyHandler; PART_SecondAuxiliaryButton.KeyDown += secondAuxKeyHandler; PART_NegativeButton.Click += negativeHandler; PART_AffirmativeButton.Click += affirmativeHandler; PART_FirstAuxiliaryButton.Click += firstAuxHandler; PART_SecondAuxiliaryButton.Click += secondAuxHandler; KeyDown += escapeKeyHandler; return(tcs.Task); }