EventAggregator模式:如何传递数据?

发布于 2024-11-09 06:28:22 字数 573 浏览 3 评论 0原文

我正在考虑在我正在开发的 UI 项目(Silverlight/MVVM)上利用事件聚合器模式来支持视图/视图模型之间的松散耦合。我们已经使用一个简单的事件系统编写了几个屏幕——此时它只不过是一个概念证明,但它看起来确实是创建松散耦合系统的一个非常优雅的解决方案。

我遇到的问题是,当接收 ViewModel 尚未实例化时,将数据从一个 ViewModel 传递到另一个 ViewModel。例如,如果客户列表屏幕打开,用户可以单击特定客户以打开编辑/查看客户详细信息屏幕(可能在子窗口中)。客户详细信息屏幕需要客户(或客户 ID)才能正常运行。

因此,OpenCustomerDetailEvent 包含相关客户信息似乎是合理的,但是有哪些选项可以将该信息注入客户详细信息 V/VM 中?由于处理事件时它尚未在内存中,因此它无法直接侦听事件(它本身不能成为处理程序)。我们使用依赖注入容器来创建视图和视图模型(我们目前执行“先查看”),因此使用单独的处理程序通过构造函数注入数据并不是一个好的选择。

构建视图(模型)后,将数据放入视图(模型)中还有哪些其他选项?带有强类型参数的 init 方法?使用setter来注入数据?让处理程序创建 V/VM,然后调度 VM 侦听的第二个事件?

I am looking at utilizing the Event Aggregator Pattern on a UI project I am working on (Silverlight/MVVM) to support loose coupling between views/viewmodels. We have written a few screens using a simple eventing system--it's not much more than a proof of concept at this point, but it does seem like a very elegent solution to creating a loosely coupled system.

The problem I am running up against is passing data from one ViewModel to another when the receiving ViewModel is not instantiated yet. For example, if a Customer List screen is open, a user might click a specific customer to open an edit/view Customer Detail screen (perhaps in a child window). The Customer Detail screen needs a customer (or customer Id) in order to function.

So is seems reasonable that the OpenCustomerDetailEvent would contain the relevant customer information, but what are some options for injecting that information into the Customer Detail V/VM? Since it's not in memory yet when the event is handled, it can't directly listen for the event (it can't itself be the handler). We are using a depenency injection container to create both the view and view model (we currently do "view first"), so having a separate handler inject the data via the constructor is not a great option.

What are some other options for getting the data into the View(Model) once it's constructed? An init method that takes strongly typed parameters? Using a setter to inject the data? Having the handler create the V/VM and then dispatch a second event that the VM listens for?

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

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

发布评论

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

评论(1

感情废物 2024-11-16 06:28:22

为此,我在 silverlight 应用程序中使用 Prism 框架。它提供了 EventAggregator 模式的实现和视图导航基础结构。

对于 prism,您将使用 NavigationParameters,它为视图/视图模型提供要使用和初始化哪些数据的信息。如果视图/视图模型尚未创建,则 EventAggregator 模式是无用的,因为它尚未注册到事件。

如果您想依赖自己的框架,我会看看棱镜框架如何 视图导航 在那里实现,并为您自己的框架挑选一些部分。

就我个人而言,我鼓励您使用棱镜框架。

For this purpose i´m using the Prism Framework in my silverlight application. It provides an implementation of the EventAggregator pattern and a view navigation infrastructure.

In the case of prism, you will use NavigationParameters, that provide information for a view/viewmodel what data to use and to initialize. If the view/viewmodel isn´t created yet, the EventAggregator pattern is useless, cause it isn´t registered to the event yet.

If you want to rely on your own framework, i would take a look at the prism framework how view navigation is implemented there and to pick up some pieces for your own framework.

Personally i would encourage you, to use the prism framework.

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