【问题标题】:UWP - exclude navigation from back button stackUWP - 从后退按钮堆栈中排除导航
【发布时间】:2016-03-31 15:38:53
【问题描述】:

我有 3 页。 A、B 和 C 页。

情况如下:

在所有 3 个屏幕上,我正在处理一个对象 e。 G。 Car。那些 to 按钮意味着如果我单击这样的按钮,我将通过 Frame.Navigate(typeof(...), Car) 传递 Car 引用导航到页面。按下后退按钮时,我只想返回而不传递任何参数。

问题是当我按下 to C 然后 to B e。 G。 5 次,然后当从页面B 通过后退按钮导航时,它会像这样。 C -> B -> C -> B -> C -> B -> C -> B -> A.

我的问题是:有没有办法不将导航添加到后退按钮堆栈?所以它只有一种方式 C -> B -> A,或 B -> A,或 C -> B。

我的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();
    }
}

提前谢谢你。

【问题讨论】:

    标签: c# win-universal-app windows-10


    【解决方案1】:

    基本上你不想在BackStack 中重复。每当发生导航时,您都可以执行以下操作。

    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】:

      实际上,your answer 给了我解决方法的想法。我只是稍微修改了代码。

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

      我只检索返回堆栈中的第一个不同项,然后我只将这个项设置到堆栈跟踪。

      【讨论】:

      • 我认为它的破坏性很小,因为您在清除它之前只是从BackStack 获得第一个项目。如果你去 A -> B -> C,你不能通过调用 this.Frame.GoBack()去 C -> B