UWP - exclude navigation from back button stack

别等时光非礼了梦想. 提交于 2020-01-25 03:13:05

问题


I have 3 pages. Page A, B and C.

The situation looks like this:

On all 3 screens I'm working with one object e. g. Car. Those to buttons mean that if I click such a button, I will navigate to the page by Frame.Navigate(typeof(...), Car) passing the Car reference. On back button press I want to just go back without passing any parameters.

The problem is that when I press to C and then to B e. g. 5 times, then when navigating via back button from page B it goes like this. C -> B -> C -> B -> C -> B -> C -> B -> A.

My question is: Is there a way not to add navigation to back button stack? So it goes only one way C -> B -> A, or B -> A, or C -> B.

My App.xaml.cs

protected override void OnLaunched(LaunchActivatedEventArgs e) {

    Frame rootFrame = Window.Current.Content as Frame;

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (rootFrame == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();

        rootFrame.NavigationFailed += OnNavigationFailed;
        rootFrame.Navigated += OnNavigated;

        if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            //TODO: Load state from previously suspended application
        }

        // Place the frame in the current Window
        Window.Current.Content = rootFrame;

        // Register a handler for BackRequested events and set the
        // visibility of the Back button
        SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;

        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
            rootFrame.CanGoBack ?
            AppViewBackButtonVisibility.Visible :
            AppViewBackButtonVisibility.Collapsed;
    }

    if (rootFrame.Content == null)
    {
        // When the navigation stack isn't restored navigate to the first page,
        // configuring the new page by passing required information as a navigation
        // parameter
        rootFrame.Navigate(typeof(MainPage), e.Arguments);
    }
    // Ensure the current window is active
    Window.Current.Activate();
}

    private void OnNavigated(object sender, NavigationEventArgs e) {
        // Each time a navigation event occurs, update the Back button's visibility
        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
            ((Frame)sender).CanGoBack ?
            AppViewBackButtonVisibility.Visible :
            AppViewBackButtonVisibility.Collapsed;
    }


private void OnBackRequested(object sender, BackRequestedEventArgs e) {
    Frame rootFrame = Window.Current.Content as Frame;

    if (rootFrame.CanGoBack) {
        e.Handled = true;
        rootFrame.GoBack();
    }
}

Thank you in advance.


回答1:


Basically you don't want repetition in BackStack. Here's what you can do whenever navigation happens.

if (this.Frame.BackStackDepth > 1)
{
    var distinctItems = this.Frame.BackStack.Distinct().ToList();
    this.Frame.BackStack.Clear();
    foreach (var item in distinctItems)
    {
        this.Frame.BackStack.Add(item);
    }
}



回答2:


Actually, your answer gave me the idea how to solve it. I just modified the code a little.

if (this.Frame.BackStackDepth > 1) {
    var firstItem = this.Frame.BackStack.Distinct().ToList().First();
    this.Frame.BackStack.Clear();
    this.Frame.BackStack.Add(firstItem);
}

I retrieve only the first distinct item in back stack and then I set only this one to the stack trace.



来源:https://stackoverflow.com/questions/36326032/uwp-exclude-navigation-from-back-button-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!