0

当我按下我的 uwp 应用程序中的硬件后退按钮时,应用程序将关闭。我使用模板 10 中的汉堡包界面。

我在 app.xaml.cs 和 het schell.xaml.cs 中添加了以下代码,但是当我按下返回时,它说参数 canGoBack 为 false 并关闭应用程序。

public Shell(INavigationService navigationService)
{
    Instance = this;
    InitializeComponent();

    // setup for static calls
    Window = WindowWrapper.Current();
    MyHamburgerMenu.NavigationService = navigationService;

    // any nav change, reset to normal
    navigationService.FrameFacade.Navigated += (s, e) =>
        BusyModal.IsModal = LoginModal.IsModal = false;

    SystemNavigationManager.GetForCurrentView().BackRequested += Shell_BackRequested;
}

private void Shell_BackRequested(object sender, BackRequestedEventArgs e)
{
    MyHamburgerMenu.NavigationService.GoBack();
}
4

1 回答 1

0

这是您应该如何处理默认实现的 BackRequested 事件:

SystemNavigationManager.GetForCurrentView().BackRequested += (sender, e) =>
{
    if (!e.Handled && Frame.CanGoBack)
    {
        e.Handled = true;
        AppFrame.GoBack();
    }
};

请记住,要使 CanGoBack 为真,您应该首先调用 Frame.Navigate()

如果 Frame.BackStack 中有帧,则 CanGoBack 将为真。

于 2016-03-29T18:53:54.447 回答