I have an app that needs to get user's attention when user exit the app at least once. So I get the code below to show a messagebox. What I don't know is how do I really exit the app if user has read the message? Because it seems the back key event will always come to the call I setup (OnBackKeyPress) Or what is a good way to handle showing a messagebox without messing around overriding BackKey? Because if have another pop up on screen and user pressed back key, it seems I got some exception if I tried to handle backkey myself.
My ideal situation is the app will close immediately once s/he pressed my Exit button of the messagebox. If pressed cancel, it will go back without exiting. Please help. Thanks!
Something I used... but not working well
private void OnBackKeyPressed(object sender, CancelEventArgs e)
{
e.Cancel = true;
CheckBox checkBox = new CheckBox()
{
Content = "Do not ask me again",
Margin = new Thickness(0, 14, 0, -2)
};
TiltEffect.SetIsTiltEnabled(checkBox, true);
CustomMessageBox messageBox = new CustomMessageBox()
{
Caption = "Would you like to stop and exit?",
Message =
"If you want to continue listen music while doing other stuff, please use Home key instead of Back key",
Content = checkBox,
LeftButtonContent = "Exit",
RightButtonContent = "Cancel",
};
messageBox.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.LeftButton: //Exit
return;// What to do here??
case CustomMessageBoxResult.RightButton: //Cancel
case CustomMessageBoxResult.None:
break;
default:
break;
}
};
messageBox.Show();
}
}
If you really need the button to say "Exit" you would have to use either ColinE or Paul Annetts solution. Personally I would try formulating the message in another way and use the normal message box like this:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
string caption = "Stop music and exit?";
string message ="If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?";
e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption, MessageBoxButton.OKCancel);
base.OnBackKeyPress(e);
}
This will show a message box but with "Ok" and "Cancel" button instead and will exit directly if user presses "Ok".
And for the "Do not ask again", maybe add a settings on a setting page instead. And add a second dialog the first time the user choose between "Ok" and "Cancel" if he or she wants to see the dialog again.