internal Task <LoginDialogData> WaitForButtonPressAsync() { Dispatcher.BeginInvoke(new Action(() => { this.Focus(); if (string.IsNullOrEmpty(PART_TextBox.Text) && !ShouldHideUsername) { PART_TextBox.Focus(); } else { PART_TextBox2.Focus(); } })); TaskCompletionSource <LoginDialogData> tcs = new TaskCompletionSource <LoginDialogData>(); RoutedEventHandler negativeHandler = null; KeyEventHandler negativeKeyHandler = null; RoutedEventHandler affirmativeHandler = null; KeyEventHandler affirmativeKeyHandler = null; RoutedEventHandler registrationHandler = null; KeyEventHandler registrationKeyHandler = null; KeyEventHandler escapeKeyHandler = null; Action cleanUpHandlers = null; var cancellationTokenRegistration = DialogSettings.CancellationToken.Register(() => { cleanUpHandlers(); tcs.TrySetResult(null); }); cleanUpHandlers = () => { PART_TextBox.KeyDown -= affirmativeKeyHandler; PART_TextBox2.KeyDown -= affirmativeKeyHandler; this.KeyDown -= escapeKeyHandler; PART_NegativeButton.Click -= negativeHandler; PART_AffirmativeButton.Click -= affirmativeHandler; RegistrationButton.Click -= registrationHandler; PART_NegativeButton.KeyDown -= negativeKeyHandler; PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler; RegistrationButton.KeyDown -= registrationKeyHandler; cancellationTokenRegistration.Dispose(); }; escapeKeyHandler = (sender, e) => { if (e.Key == Key.Escape) { cleanUpHandlers(); tcs.TrySetResult(null); } }; negativeKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(new LoginDialogData { Username = Username, Password = PART_TextBox2.Password, ButtonAction = null, ShouldRemember = RememberCheckBoxChecked }); } }; affirmativeKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(new LoginDialogData { Username = Username, Password = PART_TextBox2.Password, ButtonAction = 1, ShouldRemember = RememberCheckBoxChecked }); } }; registrationKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(new LoginDialogData { Username = Username, Password = PART_TextBox2.Password, ButtonAction = 2, ShouldRemember = RememberCheckBoxChecked }); } }; negativeHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(new LoginDialogData { Username = Username, Password = PART_TextBox2.Password, ButtonAction = null, ShouldRemember = RememberCheckBoxChecked }); e.Handled = true; }; affirmativeHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(new LoginDialogData { Username = Username, Password = PART_TextBox2.Password, ButtonAction = 1, ShouldRemember = RememberCheckBoxChecked }); e.Handled = true; }; registrationHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(new LoginDialogData { Username = Username, Password = PART_TextBox2.Password, ButtonAction = 2, ShouldRemember = RememberCheckBoxChecked }); e.Handled = true; }; PART_NegativeButton.KeyDown += negativeKeyHandler; PART_AffirmativeButton.KeyDown += affirmativeKeyHandler; RegistrationButton.KeyDown += registrationKeyHandler; PART_TextBox.KeyDown += affirmativeKeyHandler; PART_TextBox2.KeyDown += affirmativeKeyHandler; this.KeyDown += escapeKeyHandler; PART_NegativeButton.Click += negativeHandler; PART_AffirmativeButton.Click += affirmativeHandler; RegistrationButton.Click += registrationHandler; return(tcs.Task); }
internal Task <LoginDialogData> WaitForButtonPressAsync() { Dispatcher.BeginInvoke(new Action(() => { this.Focus(); if (string.IsNullOrEmpty(PART_TextBox.Text)) { PART_TextBox.Focus(); } else { PART_TextBox2.Focus(); } })); TaskCompletionSource <LoginDialogData> tcs = new TaskCompletionSource <LoginDialogData>(); RoutedEventHandler negativeHandler = null; KeyEventHandler negativeKeyHandler = null; RoutedEventHandler affirmativeHandler = null; KeyEventHandler affirmativeKeyHandler = null; KeyEventHandler escapeKeyHandler = null; Action cleanUpHandlers = () => { PART_TextBox.KeyDown -= affirmativeKeyHandler; PART_TextBox2.KeyDown -= affirmativeKeyHandler; this.KeyDown -= escapeKeyHandler; PART_NegativeButton.Click -= negativeHandler; PART_AffirmativeButton.Click -= affirmativeHandler; PART_NegativeButton.KeyDown -= negativeKeyHandler; PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler; }; escapeKeyHandler = (sender, e) => { if (e.Key == Key.Escape) { cleanUpHandlers(); tcs.TrySetResult(null); } }; negativeKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(null); } }; affirmativeKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(new LoginDialogData { Username = Username, Password = PART_TextBox2.Password }); } }; negativeHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(null); e.Handled = true; }; affirmativeHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(new LoginDialogData { Username = Username, Password = PART_TextBox2.Password }); e.Handled = true; }; PART_NegativeButton.KeyDown += negativeKeyHandler; PART_AffirmativeButton.KeyDown += affirmativeKeyHandler; PART_TextBox.KeyDown += affirmativeKeyHandler; PART_TextBox2.KeyDown += affirmativeKeyHandler; this.KeyDown += escapeKeyHandler; PART_NegativeButton.Click += negativeHandler; PART_AffirmativeButton.Click += affirmativeHandler; return(tcs.Task); }
internal Task <SigninDialogData> WaitForButtonPressAsync() { Dispatcher.BeginInvoke(new Action(() => { Focus(); if (string.IsNullOrEmpty(PART_TextBox.Text)) { PART_TextBox.Focus(); } else { PART_TextBox2.Focus(); } })); TaskCompletionSource <SigninDialogData> tcs = new TaskCompletionSource <SigninDialogData>(); RoutedEventHandler signupHandler = null; KeyEventHandler signupKeyHandler = null; RoutedEventHandler signinHandler = null; KeyEventHandler signinKeyHandler = null; RoutedEventHandler cancelHandler = null; KeyEventHandler cancelKeyHandler = null; KeyEventHandler escapeKeyHandler = null; Action cleanUpHandlers = null; var cancellationTokenRegistration = DialogSettings.CancellationToken.Register(() => { cleanUpHandlers(); tcs.TrySetResult(null); }); cleanUpHandlers = () => { PART_TextBox.KeyDown -= signinKeyHandler; PART_TextBox2.KeyDown -= signinKeyHandler; this.KeyDown -= escapeKeyHandler; PART_SignupButton.Click -= signupHandler; PART_SigninButton.Click -= signinHandler; PART_CancelButton.Click -= cancelHandler; PART_SignupButton.KeyDown -= signupKeyHandler; PART_SigninButton.KeyDown -= signinKeyHandler; PART_CancelButton.KeyDown -= cancelKeyHandler; cancellationTokenRegistration.Dispose(); }; escapeKeyHandler = (sender, e) => { if (e.Key == Key.Escape) { cleanUpHandlers(); tcs.TrySetResult(null); } }; signupKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(new SigninDialogData { ShouldSignup = true }); } }; signupHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(new SigninDialogData { ShouldSignup = true }); e.Handled = true; }; signinKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { var isValid = IsUsernameValid(Username) && IsPasswordValid(Password); if (isValid) { cleanUpHandlers(); tcs.TrySetResult(new SigninDialogData { Username = Username, Password = PART_TextBox2.Password, ShouldSignup = false }); } } }; signinHandler = (sender, e) => { var isValid = IsUsernameValid(Username) && IsPasswordValid(Password); if (isValid) { cleanUpHandlers(); tcs.TrySetResult(new SigninDialogData { Username = Username, Password = PART_TextBox2.Password, ShouldSignup = false }); e.Handled = true; } }; cancelKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(null); } }; cancelHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(null); e.Handled = true; }; PART_SignupButton.KeyDown += signupKeyHandler; PART_SigninButton.KeyDown += signinKeyHandler; PART_CancelButton.KeyDown += cancelKeyHandler; PART_TextBox.KeyDown += signinKeyHandler; PART_TextBox2.KeyDown += signinKeyHandler; KeyDown += escapeKeyHandler; PART_SignupButton.Click += signupHandler; PART_SigninButton.Click += signinHandler; PART_CancelButton.Click += cancelHandler; return(tcs.Task); }