UWP - 从后退按钮堆栈中排除导航 [英] UWP - exclude navigation from back button stack

查看:30
本文介绍了UWP - 从后退按钮堆栈中排除导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 页.页面 A、B 和 C.

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

情况如下:

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

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.

问题是当我按到C然后按到B时.G.5 次,然后从页面 B 通过后退按钮导航时,它是这样的.C -> B -> C -> B -> C -> B -> C -> B -> A.

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.

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

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.

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

提前致谢.

推荐答案

实际上,您的回答让我知道如何来解决它.我只是稍微修改了代码.

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.

这篇关于UWP - 从后退按钮堆栈中排除导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆