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;
}