I have an observable that I use for displaying a confirmation dialog, roughly of signature:
IObservable<DialogResult> ShowDialog(string title, string message);
This shows the user the dialog, with a yes / no button combo.
In my main window, I'm accessing the closing event as such:
this.Events().Closing.[Do something here]
I want to be able to:
- Show the confirmation dialog when the closing observable fires
- Set the
CancelEventArgs.Cancel
property to true if the user clicks the "no" button.
I've tried straight-up subscribing:
this.Events().Closing.Subscribe(e =>
{
var res = Dialogs.ShowDialog("Close?", "Really Close?").Wait();
e.Cancel = res == DialogResult.Ok;
});
But that hangs because of the Wait()
call, I've also tried an async variant:
this.Events().Closing.Subscribe(async e =>
{
var res = await Dialogs.ShowDialog("Close?", "Really Close?");
e.Cancel = res == DialogResult.Ok;
});
Which is no good, because it just returns right away.
What I really want to do is something like:
this.Events().Closing.ThenDo(_ => Dialogs.ShowDialog("Close?", "Really Close?"))
.Subscribe((cancelEventArgs, dialogResult) =>
{
cancelEventArgs.Cancel == dialogResult == DialogResult.Ok;
});
But that doesn't exist, I know the key here is in how I combine the two observables, but I've no idea how to do so, and the documentation is a little light on practical examples.