Derik Whittaker

Syndication

News


Catching the Back Button action on WP7

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.

Sample method

        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,


Posted 09-02-2010 6:58 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Derik Whittaker wrote Performing Back Navigation w/out adding a page to the Back Stack in WP7
on 09-03-2010 7:39 PM

In a previous post ( here ) I wrote about how you can catch the back button action on WP7.  In this

Oleksandr Krakovetskiy blog wrote Дайджест технических материалов #5 (Windows Phone 7)
on 10-27-2010 7:15 AM

Tools, Books, Guides Windows Phone 7 Developer Tools RTM (online installer) , ISO UI Design and Interaction

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)