复合 WPF EventAggregator 订阅丢失

发布于 2024-07-27 00:09:44 字数 244 浏览 4 评论 0原文

在我的复合 WPF 应用程序中,当用户双击控件时会发布一个事件。 模块订阅事件并在必要时执行操作。

该事件似乎随机停止工作。 有时,当我运行应用程序时,我可以毫无问题地触发事件,而其他时候,我只能在模块停止接收事件之前触发几次。

当我查看调试器时,CAL EventAggregator 仍然有该事件,但该事件没有订阅。 EventAggregator 怎么会失去订阅?

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.

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?

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

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

发布评论

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

评论(1

三岁铭 2024-08-03 00:09:44

原来是垃圾收集器删除了订阅。 我必须阅读内部结构,但是当我替换

this.mEventAggregator.GetEvent<SomeEvent>().Subscribe(SomeFunction);

this.mEventAggregator.GetEvent<SomeEvent>().Subscribe(
    SomeFunction, ThreadOption.UIThread, true);

时它开始工作。 UI 线程参数不是我的问题,但对于其他人来说,确保您也在正确的线程上处理事件可能很重要。

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);

with

this.mEventAggregator.GetEvent<SomeEvent>().Subscribe(
    SomeFunction, ThreadOption.UIThread, true);

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.

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