如何确定谁在调用 prism eventaggregator 订阅?或多个订阅调用

发布于 2024-12-03 20:40:30 字数 312 浏览 0 评论 0原文

我在一个视图模型中有一个订阅方法,用于监听另一个视图模型中的发布事件。由于某种原因,我无法弄清楚,当发布方法被触发时,订阅方法会运行多次(在我的例子中每次运行四次)。我在每个发布上都有断点,除了第一个发布调用之外,没有一个发布调用被触发。

当订阅多个调用发生时,调用堆栈不会改变。

我如何找出导致多次订阅调用发生的原因?

更新:当我发现它时,结果发现这是一个“doh”答案。所讨论的视图模型是表/网格行的视图模型的集合,自然地,添加到网格的行越多,添加到事件聚合器的订阅就越多。我需要实现一些取消订阅逻辑来删除我不想要的内容。

I have a subscribe method in one view model listening for publish events in another view model. For some reason, that I can't figure out, when the publish method is fired, the subscribe method runs multiple times (in my case four each time). I have breakpoints on each publish and other than the first publish call, none of them are firing.

The call stack doesn't change as the subscribe multiple calls happen.

How can I figure out what is causing the multiple calls to subscribe to happen?

UPDATE: Turned out that this was a 'doh' answer when I found it. The viewmodel's in question were a collection of viewmodels for the lines of a table/grid and naturally, the more rows that got added to the grid, the more subscribes were added to the event aggregator. I needed to implement some unsubscribe logic to get rid of the ones I didn't want.

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

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

发布评论

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

评论(1

春花秋月 2024-12-10 20:40:30

如果您使用依赖注入,您可以创建一个虚拟包装类来满足 IEventAggregator 并路由到 EventAggregator 并捕获那里的所有订阅调用吗?我已经看到这种行为发生在 ViewModel 实例化与撕下选项卡控件相结合的订阅中。

If you're using dependency injection with this, could you create a dummy wrapper class that satisfies IEventAggregator and routes to EventAggregator and catch all the subscribe calls there? I've seen this sort of behavior happen with subscription that happens on ViewModel instantiation combined with tear off tab controls.

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