具有持久订阅者行为的 WPF/SL EventAggregator 实现?

发布于 2024-08-27 02:01:05 字数 767 浏览 4 评论 0原文

目前我正在使用最新的 Prism for Silverlight 4 构建一个应用程序。 我有一个模块,在该模块中我有两个带有视图模型的视图。我还有一个模块视图,每个视图有两个区域。 在模块初始化中,我在 Unity 容器中注册视图和视图模型,并向相应区域注册视图。 问题是视图应该显示类似于表详细信息的内容 - 第一个视图显示可用实体,第二个视图显示所选实体的详细信息。

我需要一种方法来传递它们初始选择的实体。新创建的第一个视图没有任何选定的实体,新创建的第二个视图不显示任何详细信息。

目前我正在这样做: 在模块中,我创建两个视图模型并将它们注册为 Unity 容器中的实例,然后将视图注册为相应区域的类型。每个视图都从 EventAggregator 订阅 EntitySelectedEvent。模块初始化程序在初始化后发布此事件,这样两个视图就会选择相同的实体。

我知道这看起来很难看 - 我尝试从一个视图模型发布此事件,但问题是 Prism 中的 EventAggregator 不支持持久订阅者 - 这意味着如果第二个视图模型没有在第一个视图模型之前订阅事件解雇它,它不会接收和事件。我知道这是 EventAggregator 的正常行为,但我正在寻找一种解决方案,当视图模型可以触发事件而不依赖于它们的初始化顺序时 - 即第一个模型可以在创建第二个模型和第二个模型之前触发事件订阅后将收到此“排队”事件。

WPF/SL 是否有任何其他消息实现确实支持这种行为,或者使用中介器(在我的示例中它本身就是一个模块)毕竟不是一个坏主意?中介者的一个大问题是模型必须在初始化时立即创建,并且它们不能在容器中注册为类型,因为这会再次导致订阅者丢失。

Currently I'm building an application using latest Prism for Silverlight 4.
I've a module and in that module I've two views with view models. Also I've a module view with two regions for each view.
In module initialization I'm registering my views and view models in Unity container and also register views with corresponding regions.
The problem is that views should display something similar to table-detail information - first view shows available entities ans the second view shows detail of selected entity.

I need a way how to pass them initial selected entity. Newly created first view doesn't have any selected entity and newly created second view doesn't show any details.

Currently I'm doing that this way:
In module I create two view models and register them as instances in Unity container and then I register views as types for corresponding regions. Each view subscribes to EntitySelectedEvent from EventAggregator. Module initializer publish this event after initialization and this way two views are selecting the same entity.

I know this looks ugly - I tried publishing this event from one of view models but the problems is that EventAggregator in Prism doesn't support durable subscribers - this means that if the second view model didn't subscribe to event before the first view model fired it, it won't receive and event. I know this is a normal behavior of EventAggregator, but I'm looking for a solution when view models can fire events without depending on initialization order of them - that is the first model can fire event before the second model was created and the second model will receive this 'queued' event after subscribing to it.

Are there any other messaging implementations for WPF/SL which do support such behavior or using a mediator (in my example it's a module itself) isn't such a bad idea after all? One big problem with mediator is that models must be created right away in initialize and they can't be registered as types in container because this leads again to missing subscribers.

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

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

发布评论

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

评论(1

把梦留给海 2024-09-03 02:01:05

创建一个模型,该模型将由每个视图的 ViewModel 共享。

当在视图 1 中选择一行时,其 ViewModel(通过绑定到所选行的 CurrentEntity 属性)将更新模型。

View 2 的 ViewModel 将订阅 Model 的 CurrentEntity 的更改,并将正确更新其自己的 CurrentEntity 属性,这将导致其 View 更新。

Create a Model that will be shared by ViewModels of each of the views.

When a row gets selected in View 1 its ViewModel (via a CurrentEntity property bound to a selected row) will update the Model.

ViewModel of View 2 would subscribe to changes of Model's CurrentEntity and will properly update it's own CurrentEntity property that would cause it's View to update.

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