Use This method:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
//Do your work here
e.Cancel = true;
var result = MessageBox.Show("Do you want to exit?", "Attention!",
MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
return;
}
e.Cancel = true;
// base.OnBackKeyPress(e);
}