如何处理仅在特定页面上的后退按钮?
问题描述:
简而言之,我只想在特定页面上处理返回按钮以返回到上一页,但在其余页面上忽略(让默认操作/关闭应用程序)。如何处理仅在特定页面上的后退按钮?
所以对于我的例子,我想只处理返回按钮CurrentPage。所以,我把我的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;
}
这个代码,但是,当我坐进|上一页,按后退按钮将再次去|上一页。我无法退出该应用程序。
如果我将其添加到PreviousPage.xaml.cs中,则从CurrentPage中按BackButton将导致返回到PreviousPage并自动关闭该应用程序。
public PreviousPage()
{
this.InitializeComponent();
SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
}
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
Application.Current.Exit();
e.Handled = true;
}
你有什么想法如何处理单个页面上的后退按钮吗?
答
当你离开当前页面时,你还应该删除后退按下的事件处理程序SystemNavigationManager.GetForCurrentView().BackRequested -= App_BackRequested;
。没有这个代码就会被调用,即使你已经离开了页面。
我没有注意到,谢谢,它解决了我的问题。 – kurakura88