In short, I would like to handle the back button only on certain page to go back to previous page, but ignore (let default operation/ close the app) on the rest of the pages.
So for my example, I would like to handle back button only for CurrentPage. So, I put this code on my CurrentPage.xaml.cs
public CurrentPage()
{
this.InitializeComponent();
SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
}
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
this.Frame.Navigate(typeof(PreviousPage), e);
e.Handled = true;
}
But, when I got into PreviousPage, pressing Back Button will go to PreviousPage again. I cannot quit the app.
If I add this on the PreviousPage.xaml.cs, then pressing BackButton from CurrentPage will result to go back to PreviousPage and close the app automatically.
public PreviousPage()
{
this.InitializeComponent();
SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
}
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
Application.Current.Exit();
e.Handled = true;
}
Do you have any idea how to handle back button on single page?
When you leave the CurrentPage, you should also remove the back pressed event handler SystemNavigationManager.GetForCurrentView().BackRequested -= App_BackRequested;
. Without this the code gets called even if you've left the page already.
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.