简体   繁体   中英

Universal App / Windows phone, OnBackKeyPressed equivalent

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM