0
votes

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();
}
1
You need to set e.Handled = true in Shell_BackRequested() accordingly. - Gaurav
The app doesn't close annymore if I add e.handled = true, but it doesn't navigate back either - A. Bouthoorn
Yup you shouldn't do that. When CanGoBack is true you can set e.handled=true. - Archana
But why is my CanGoBAck Always false? - A. Bouthoorn

1 Answers

0
votes

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.