复合WPF EventAggregator订阅丢失 [英] Composite WPF EventAggregator subscriptions being lost
问题描述
在我的Composite WPF应用程序中,当用户双击控件时,我发布了一个事件。模块订阅该事件并在必要时执行操作。
In my Composite WPF application I have an event that is published when the user double-clicks on a control. Modules subscribe to the event and perform an action when necessary.
此事件似乎随机停止工作。有时,当我运行应用程序时,我可以触发事件没有问题,其他时候,我只能触发它几次,在模块停止接收事件之前。
This event seems to stop working at random. Sometimes when I run the application I can trigger the event with no problems, other times I can only trigger it a few times before the modules stop receiving the event.
当我在调试器中查看CAL EventAggregator
仍然有事件,但事件没有订阅。
When I look in the debugger the CAL EventAggregator
still has the event, but the event has no subscriptions. How can EventAggregator
be losing subscriptions?
推荐答案
事实证明,垃圾收集器删除订阅。我必须在内部阅读,但是当我更换
Turns out it was the garbage collector removing the subscriptions. I'll have to read up on the internals, but when I replaced
this.mEventAggregator.GetEvent<SomeEvent>().Subscribe(SomeFunction);
与
this.mEventAggregator.GetEvent<SomeEvent>().Subscribe(
SomeFunction, ThreadOption.UIThread, true);
它开始工作。 UI线程参数不是我的问题,但对于其他人来说,确保您在正确的线程处理事件可能很重要。
it started working. The UI thread parameters wasn't my problem, but for others it may be important to ensure you're handling the event on the right thread too.
这篇关于复合WPF EventAggregator订阅丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!