WPF PRISM 事件订阅不会消亡

发布于 2024-10-05 06:36:48 字数 664 浏览 13 评论 0原文

我有一个 PRISM RegionManager,其中有几个区域 - 顶部的功能区区域和其下方用于我的视图的主要内容区域 - 相当基本。

该应用程序以主要内容区域中的“主页”视图开始。当我单击功能区上的按钮时,我将第二个视图注入内容区域并在区域管理器中导航到它。当我单击此视图上的按钮时,应将其删除并显示原始视图。

我目前正在使用 RegionManager.Add() 方法手动添加第二个视图。当我想要删除它时,我发布一个由管理器类使用的事件,该管理器类获取当前活动的删除,在区域管理器上调用Remove(),然后导航回原始视图。

这一切都很好,除了当我调用Remove()后第二个视图保持活动状态时,并保留它在其生命周期内进行的任何订阅的句柄!我尝试使用 false 显式调用 Subscribe 以获取弱事件引用,但无论如何这是默认设置,并且没有帮助。我尝试过创建视图(发现和注入)以及通过“删除”和“停用”删除视图的两种类型。他们都没有帮忙。这是一个真正的问题,因为当我想在将来创建相同的视图(该视图的新实例)时,我留下了它们的多个实例,所有实例都订阅相同的事件,即使其中一些实例是很久以前就被从区域经理中删除了。

我是否需要手动取消订阅我在视图中订阅的每个事件(非常不可取)?或者是否有某种方法可以处理该控件/将其标记为已停用,以便删除其订阅?

我感觉我在这里做了一些愚蠢的事情,但这让我完全陷入困境。

谢谢

I have a PRISM RegionManager with a couple of region - a Ribbon region on the top, and a main content region for my view underneath it - fairly basic.

The app starts with a "home" view in the main content area. When I click a button on the Ribbon, I inject a second view into the content area and navigate to it in the region manager. When I click a button on this view, it should be removed and the original view should be shown.

I'm currently doing this using the RegionManager.Add() method to manually add the second view. When I want to remove it, I publish an event which is consumed by a manager class that gets the current active remove, calls Remove() on the Region Manager for it, and then navigates back to the original view.

This all works great, except that when the second view is left alive after I call Remove(), and keeps a handle onto any subscriptions that it made during its lifetime! I've tried calling Subscribe explicitly with false for weak event references, but this is the default anyway, and it didn't help. I've tried both types of creating the view (discovery and injection) and removing the view via Remove and Deactivate. None of them helped. This is a real problem as when I want to create the same view in the future (a new instance of that view), I'm left with several instances of them, all subscribing to the same events, even though some of those instances were removed ages ago from the Region Manager.

Do I need to manually unsubscribe from every event that I subscribe to in my views (highly undesirable)? Or is there some way that I can dispose of the control / mark it as deactivated so that its subscriptions get removed?

I get the feeling that I'm doing something silly here but it's got me completely stumped.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-10-12 06:36:48

这是一个已知问题。 eventAggregator 代码中存在错误。看看下面的博客和建议的克服它的技巧
https://greenicicle.wordpress .com/2010/04/28/prism-event-aggregator-more-leaky-than-it-seems/

希望这有帮助

This is a known issue. There is a bug in the eventAggregator code. Take a look at the following blog and the suggested hack to overcome it
https://greenicicle.wordpress.com/2010/04/28/prism-event-aggregator-more-leaky-than-it-seems/

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文