Universal App / Windows phone, OnBackKeyPressed equivalent

206 Views Asked by At

I am trying to get the back button to go back to the previous page instead of closing the app. Until now, I had no luck. Its a universal app but I'm making the windows phone 8.1 first. I have the following code but its not working!

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    this.Frame.Navigate(typeof(MainPage));
    base.OnBackKeyPress(e);
}

this is the error I get:

Error 3 'Windows.UI.Xaml.Controls.Page' does not contain a definition for 'OnBackKeyPress'

1

There are 1 best solutions below

0
On

if the code is inside the shared folder, try put the code inside

#if WINDOWS_PHONE_APP
protected override void OnBackKeyPressed(...)
...
#endif

The error occurs because there is no definition for OnbackKeyPress in the Windows-App-Project. So you have to define this for the Phone Project only. Putting the code inside a file that is in the Phone-Project would also work.

Hope it helps