通用 Windows 10 个应用程序。如何使用后退按钮? VB.NET

Universal Windows 10 App. How to use Back Button? VB.NET

在我的应用程序中,如果在子页面上按下后退按钮,应用程序将关闭。

如何在按下后退按钮时调用 Frame.Navigate?

我看过如何在 C# 中执行此操作的示例,但这些示例似乎不能很好地转换为 VB.NET。

下面是我试过的方法

Private Sub OnHardwareButtonsBackPressed(sender As Object, e As Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested)
        Frame.GoBack()
    End Sub

这会在声明事件类型时出现问题。

也在尝试处理事件 Windows.UI.Core.SystemNaviationManager 尽管这没有成功拦截后退按钮。

正确的语法是:

Private Sub OnHardwareButtonsBackPressed(sender As Object, e As BackRequestedEventArgs)
    ' Tell the app that you have handled the event
    ' Otherwise, the back button will be processed normally and the app will close
    e.handled = true
End Sub

然后,附加您的活动:

AddHandler SystemNavigationManager.GetForCurrentView().BackRequested, AddressOf OnHardwareButtonsBackPressed

为任何寻找 VB.Net 代码来实现应用程序后退按钮的人修改了上面的建议代码,将 ff 添加到您的 Page2(从主页加载的页面)中:

  1. 添加命名空间:Windows.UI.Core.SystemNavigationManager
  2. 在 Page2 的 Loaded 事件中添加这段代码

GetForCurrentView.AppViewBackButtonVisibility = Visibility.Visible

A​​ddHandler GetForCurrentView.BackRequested、AddressOf OnAppButtonsBackPressed

  1. 添加处理程序

私有子 OnAppButtonsBackPressed()

如果Frame.CanGoBack那么Frame.Goback()

结束子