I am using MahApps and I am trying to implement an abort-dialog if a user cancels a print-process. Since I am still using .Net 4.0 I cannot use await
, but need to use continuations as stated here:
However for the life of me I cannot figure out how to do this. I have created a class DialogService
that I want to use to present dialogs, when needed. I added the method AbortPrintingDialog(ViewModelBase parentViewModel)
, where parentViewModel
is the ViewModel that wants to show the dialog. Originally I had the following for AbortPrintingDialog
, which is an adaption of code from the MahApps sample program and works as expected giving the correct output on the debug-console:
public class DialogService
{
private IDialogCoordinator dialogCoordinator;
public DialogService(IDialogCoordinator dialogCoordinator)
{
this.dialogCoordinator = dialogCoordinator;
}
public void AbortPrintingDialog(ViewModelBase parentViewModel)
{
dialogCoordinator.ShowMessageAsync(parentViewModel,
"Abort Printing",
"Printing is in progress. Are you sure you want to abort the printing process?",
MessageDialogStyle.AffirmativeAndNegative).ContinueWith(t => { Debug.WriteLine("t.Result: " + t.Result); });
}
}
I now tried to change this using continuations so that I can get the user-selected value in order to return it later on from my function AbortPrintingDialog
. So I modified AbortPrintingDialog
like this, which I thought would work after reading the code on this MSDN page:
public MessageDialogResult AbortPrintingDialog(ViewModelBase parentViewModel)
{
Task<MessageDialogResult> WaitUserInputTask = dialogCoordinator.ShowMessageAsync(parentViewModel,
"Abort Printing",
"Printing is in progress. Are you sure you want to abort the printing process?",
MessageDialogStyle.AffirmativeAndNegative);
Task.WaitAll(WaitUserInputTask);
Task<MessageDialogResult> continuation = WaitUserInputTask.ContinueWith((antecedent) =>
{
return antecedent.Result;
});
return continuation.Result;
}
However, now when I hit the abort-button in my GUI to call AbortPrintingDialog
, the GUI locks up and I don't get any error-message. So what am I doing wrong? I have been trying to figure this out and fiddling around for quite for some time now...