I'm using the NavigationHelper class in my Universal App and I've enabled the BackPressed event at the application level
#if WINDOWS_PHONE_APP
private async void HardwareButtons_BackPressed(object sender,
Windows.Phone.UI.Input.BackPressedEventArgs e)
{
...
}
#endif
In my HardwareButtons_BackPressed event, I want to prompt the user as to whether or not they are sure they want to quit the application by calling the following code:
UICommand ans = await this.GetLocatorViewModel.
DialogService.ShowMessagePrompt("Are you sure you want to quit?");
//NOTE: The validation below is not the complete code as I've removed
//the usage of my Enum for testing purpose but I would check the
//ans.Id
if (!object.ReferenceEquals(ans, null))
{
e.Handled = true;
}
I'm using the standard async method to display the prompt and it's defined as follows:
public async Task<Object> ShowMessagePrompt(string content)
{
MessageDialog msgbox = new MessageDialog(content);
msgbox.Commands.Clear();
msgbox.Commands.Add(new UICommand { Label = "Yes", Id = 0 });
msgbox.Commands.Add(new UICommand { Label = "No", Id = 1 });
return await msgbox.ShowAsync();
}
If I use with the above code, as it is called from the NavigationHelper class asynchronously, it continues on with the code and still closes the app.
If I use the msgbox.ShowAsync().Result:
object ans = this.GetLocatorViewModel.DialogService.ShowMessagePrompt("Are you sure you want to quit?").Result;
it displays the prompt ok, but once I tap Yes or No, it closes the prompt but it causes my app to hang and any code after it doesn't get executed i.e. checking for the prompt's result, etc...
Any idea how I can resolve this?
Thanks.
Set the e.Handle to true before you show the message dialog.