WPF 导航服务“RemoveBackEntry"正在删除最旧的条目,而不是最近的条目 [英] WPF NavigationService "RemoveBackEntry" is removing the oldest entry, rather than the most recent

查看:92
本文介绍了WPF 导航服务“RemoveBackEntry"正在删除最旧的条目,而不是最近的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的 WPF 应用程序,其中包含少量页面.提交表单后,我想导航到特定页面,然后清除最后一个导航条目,以便用户无法重新提交他们刚刚提交的表单.

I have a fairly simple WPF application with a handful of pages. Upon submit of a form, I'm wanting to navigate to a specific page, then clear out the last navigation entry so that the user is unable to then resubmit that form they had just submitted.

但是,当我在导航到特定页面后在导航服务上调用RemoveBackEntry()"时,它会删除返回堆栈中的第 3 个条目(在这种情况下是最旧的条目)而不是我正在导航的页面从.当新页面加载时,该页面将作为返回堆栈中的最新条目.

However, when I call "RemoveBackEntry()" on the navigation service after navigating to the specific page, it removes the 3rd entry (which is the oldest in this case) in the back stack rather than the page I'm navigating from. That page remains as the most recent entry in the back stack when the new page loads.

这是我的代码,虽然它非常简单明了.

Here's my code, although it's quite simple and straight forward.

  public void NavigateToNewWorkPage()
    {
        _view.NavigationService?.Navigate(new WorkPage());
        _view.NavigationService?.RemoveBackEntry();
    }

推荐答案

我遇到了同样的问题,并通过使用 NavigationService 提供的事件解决了它.

I had the same problem and solved it by using the events the NavigationService provides.

NavigationService.Navigate(..) 方法是异步的,当您调用 RemoveBackEntry() 时,您当前的视图还没有出现在返回条目日志中.因此,您在导航之前删除了作为最后一个返回条目的视图.你可以这样解决:

The NavigationService.Navigate(..) method is async and when you call RemoveBackEntry() your current view is not yet in the back entries journal. So you remove the view which was the last back entry before navigating. You could solve it like this:

public void NavigateToNewWorkPage()
{
    if (_view.NavigationService != null)
    {
        _view.NavigationService.Navigated += NavServiceOnNavigated;
        _view.NavigationService.Navigate(new WorkPage());
    }
}

private void NavServiceOnNavigated(object sender, NavigationEventArgs args)
{
    _view.NavigationService.RemoveBackEntry();
    _view.NavigationService.Navigated -= NavServiceOnNavigated;
}

您等待 Navigated 事件,因此您从中导航的视图成为最后一个返回条目,然后您将其删除.

You wait for the Navigated event so the view you navigate from became the last back entry and then you remove it.

这篇关于WPF 导航服务“RemoveBackEntry"正在删除最旧的条目,而不是最近的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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