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.


Discover more from Dhananjay Kumar

Subscribe to get the latest posts sent to your email.

Published by Dhananjay Kumar

Dhananjay Kumar is founder of NomadCoder and ng-India

5 thoughts on “Code to disable hardware Back Button in Windows Phone 7

Leave a comment

Discover more from Dhananjay Kumar

Subscribe now to keep reading and get access to the full archive.

Continue reading