I want to show a dialog to confirm exit when user presses back key. I know it can be done by using the native MessageBox. However, when I use CustomMessageBox from winphone toolkit instead, the cancel event isn't resumed once it is canceled.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
CustomMessageBox messageBox = new CustomMessageBox()
{
Caption = "Exit",
Message = "Do you want to exit?",
LeftButtonContent = "Yes",
RightButtonContent = "No"
};
messageBox.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.LeftButton:
//app doesn't exit as expected
e.Cancel = false;
break;
case CustomMessageBoxResult.RightButton:
break;
case CustomMessageBoxResult.None:
break;
default:
break;
}
};
messageBox.Show();
}
If I don't cancel the event, the app will exit without showing the messagebox. If I try to call Show then lock the thread, the messagebox will not be displayed. I know I can exit app by using Terminate or exception instead but I also want it to exit properly so Application_Closing can be called. Please help! Thank.
If you use the custom messagebox, then you have no other choice than canceling the back event. And if you do, then you have to exit pro grammatically the application, with the issue you mentioned.
There's no way out of that one. You need to free the UI thread so CustomMessageBox can display its controls, and that thread won't be free until you've exited the OnBackKeyPress
event. Just stick to the native messagebox, it's for the best.