【问题标题】:How to handle back button for only on a certain page?如何仅在某个页面上处理后退按钮?
【发布时间】:2016-09-21 09:04:56
【问题描述】:

简而言之,我只想在某些页面上处理后退按钮以返回上一页,而在其余页面上忽略(让默认操作/关闭应用程序)。

因此,对于我的示例,我只想为 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;
}

你知道如何处理单页上的后退按钮吗?

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    当您离开 CurrentPage 时,您还应该删除后按事件处理程序 SystemNavigationManager.GetForCurrentView().BackRequested -= App_BackRequested;。没有这个,即使你已经离开了页面,代码也会被调用。

    【讨论】:

    • 我没有注意到,谢谢,它解决了我的问题。
    猜你喜欢
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2017-05-13
    • 2011-10-26
    • 2012-09-29
    • 2012-01-24
    相关资源
    最近更新 更多