Reputation: 18520
I'm writing a windows phone app, and I want to know how to alert and make sure the that user really wants to exit the app on the back key press. Pretty simple.
Thanks.
Upvotes: 2
Views: 3208
Reputation: 69372
I assume your navigation is set up such that the user can only exit from the first page. If so, in that page, you can override the OnBackKeyPress event and cancel the button press. I haven't tested this code, but seems like it should work:
protected override void OnBackKeyPress(CancelEventArgs e)
{
if(MessageBox.Show("Are you sure you want to exit?","Exit?",
MessageBoxButton.OKCancel) != MessageBoxResult.OK)
{
e.Cancel = true;
}
}
Edit - I'll leave this here as an example of overriding the back button, but the correct answer in this context is to not implement the feature.
Upvotes: 6
Reputation: 12216
While the previous answer about cancelling OnBackKeyPress may technically work, it could cause your app to fail certification requirements. See the following link:
http://msdn.microsoft.com/en-us/library/hh184840(v=VS.92).aspx
5.2.4.2 – Back Button: First Screen Pressing the Back button from the first screen of an application must close the application.
I would recommend not implementing this functionality.
Upvotes: 2