Windows Phone 7

Code to disable hardware Back Button in Windows Phone 7

You can come across requirement when you need to disable hardware back button in Windows Phone 7.

clip_image001

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

clip_image003

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

clip_image004

If you want you can display a message to user when users try to navigate from back button. For your reference to disable back button code 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  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

About Dhananjay Kumar

Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Discussion

4 Responses to “Code to disable hardware Back Button in Windows Phone 7”

  1. I guess, disabling hardware back button will cause problem with marketplace submission.

    5.2.4.2 – Back Button: First Screen
    Pressing the Back button from the first screen of an application must close the application.

    http://msdn.microsoft.com/en-us/library/hh184840(v=VS.92).aspx

    Posted by Chintan Shah | November 11, 2011, 1:47 pm
  2. Yes , this is a bit dangerous and should be handled carefully .

    Posted by Senthil Kumar B | November 11, 2011, 11:13 pm
  3. i wish be able to disable search button. is so annoiyng. :(

    Posted by gene | February 6, 2012, 7:39 pm

Trackbacks/Pingbacks

  1. Pingback: Monthly Report November 2011: Total Posts 22 « debug mode…… - December 1, 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,380 other followers

Tweets

Categories

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2012