WPF PRISM事件订阅不会消失 [英] WPF PRISM event subcriptions not dying

查看:499
本文介绍了WPF PRISM事件订阅不会消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PRISM RegionManager,它具有几个区域-顶部是一个Ribbon区,下面是我的视图的主要内容区域-相当基本.

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

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

这一切都很好,除了在我调用Remove()之后第二个视图保持活动状态并保留其在其生命周期内进行的所有订阅的句柄之外!我已经尝试过使用弱事件引用显式调用false来订阅,但这仍然是默认设置,并且没有帮助.我尝试了两种创建视图的方式(发现和注入),也可以通过删除"和停用"来删除视图.他们都没有帮助.这是一个真正的问题,因为当我将来要创建相同的视图(该视图的新实例)时,我留下了几个实例,所有实例都订阅了相同的事件,即使其中一些实例是从区域管理器中删除了很久以前的内容.

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

我觉得自己在这里做些愚蠢的事情,但这让我完全陷入了困境.

谢谢

解决方案

这是一个已知问题. eventAggregator代码中存在错误.看看以下博客以及克服它的建议技巧 http://greenicicleblog.com/2010/04/28/prism-event-aggregator-leaky-than-it-seems/

希望这对您有帮助

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

解决方案

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 http://greenicicleblog.com/2010/04/28/prism-event-aggregator-more-leaky-than-it-seems/

Hope this helps

这篇关于WPF PRISM事件订阅不会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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