SIGN UP MEMBER LOGIN:    
ARTICLE

Code to Disable the Hardware Back Button in Windows Phone 7

Posted by Dhananjay Kumar Articles | Windows Phone in C# November 10, 2011
You may have come across a requirement where you need to disable the hardware back button in Windows Phone 7.
Reader Level:

You may have come across a requirement where you need to disable the hardware back button in Windows Phone 7. 

BckBtnWinPhn1.gif

If you want to disable the hardware back button on Page1 then you need to override OnBackKeyPress method on Page1.

BckBtnWinPhn2.gif

To disable you need to set the CancelEventArgs value to cancel as given below,

BckBtnWinPhn3.gif

If you want, you can display a message to user when users try to navigate from back button. For your reference, the code to disable the back button is given below.

using System.Windows;
using Microsoft.Phone.Controls;
 
namespace PhoneApp1
{
    public partial class SecondPage : PhoneApplicationPage
    {
        public SecondPage()
        {
            InitializeComponent();
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            //base.OnBackKeyPress(e);

            MessageBox.Show("You can not use Hardware back button");
             e.Cancel = true
        }

  }
}


When run this code and try to navigate using back button, you will get a message and hardware back button will not work. You can have any other business requirement code in overridden OnBackKeyPress method as well. I hope this post is useful. Thanks for reading.
 

Login to add your contents and source code to this article
share this article :
post comment
 

Exactly what is so horrible about the user shutting down a cell phone app that you need to disable this functionality. A far better and inoffensive thing to do is use event to trigger an orderly shut down of your app. Ever consider that the reason why the user is pressing the back key might be because your app is not working correctly and they just want to get out of it? Locking the back key can have the free bonus of enraging your users by forcing them to remove the battery to escape your program when it gets confused by some unexpected input or event. That should sell tons of programs.

Posted by Phil Ouellette Nov 28, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor