0

When I press on the hardware back button in my uwp app, the app closes. I use the hamburger interface from Template 10.

I added the following code in the app.xaml.cs and in het schell.xaml.cs but when I press back it says that the parameter canGoBack is false and closes the app.

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();
}
A. Bouthoorn
  • 123
  • 1
  • 7

1 Answers1

0

This is how you should handle the BackRequested event for a default implementation:

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

Remember that for CanGoBack to be true you should call first a Frame.Navigate()

If there are frames in the Frame.BackStack, then CanGoBack will be true.

Tommaso Scalici
  • 480
  • 1
  • 6
  • 15