Search code examples
c#windows-phone-7

Windows Phone - cancel back from mainpaga.xaml


I want to override the backbutton on mainpage. What works on other pages does not work on main. Here's my code:

protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
    {
        base.OnNavigatingFrom(e);
        if (MessageBox.Show("Wszystkie zmiany zostaną odrzucone", "Odrzucenie Zmian", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
        {
            e.Cancel = true;
        }
    }

The problem is that it shows the messagebox to confirm exit, however it is also exiting the application, so confirming or cancelling makes no sense anyway.


Solution

  • You must override OnBackKeyPress instead of OnNavigatingFrom

    protected override void OnBackKeyPress(CancelEventArgs e)
    {
        if (MessageBox.Show("Wszystkie zmiany zostaną odrzucone", "Odrzucenie Zmian", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
        {
                e.Cancel = true; 
    
        }
    }