When building WP7 applications there may be times where you want to trap the Back Button action in order to perform your own custom action. For example if you are editing some values and a user has not saved the data and presses the back button you may want to perform some action to validate the data. Or you may want to update some state about your application. The good news is that catching the back button action in WP7 is extremely easy.
In order to do this all you need to do is override the OnBackKeyPress method on your PhoneApplicationPage. When you override this you will have access to CancelEventArgs object which provides you access to the Cancel action. If you truly want to stop the user from navigating way from that page (please keep in mind that this type of action needs to be used very sparingly) all you need to do is set the e.Cancel = true and the navigation will stop.
protected override void OnBackKeyPress( System.ComponentModel.CancelEventArgs e )
e.Cancel = true;
Please keep in mind that when stopping the back action on the phone you are breaking the standard user experience and this should be done ONLY when needed.
Till next time,
09-02-2010 6:58 PM