使用 MVVM Foundation Messenger 显示对话框
我正在构建一个 WPF 应用程序并尝试遵循 MVVM 最佳实践。我正在使用 MVVM Foundation 框架并注意到 Messenger 类,我读过的应该用于 < a href="https://stackoverflow.com/questions/454868/handling-dialogs-in-wpf-with-mvvm">处理 WPF 中的对话框。这听起来不错,但我完全不明白如何使用 Messenger 来实现此目的。从字面上看,我想做的就是打开一个模式“关于”对话框——我不需要来回传递任何消息。
Messenger 类的目的是用于对话框需要来自其父级的消息或向其父级返回消息的情况吗?对于“关于”对话框来说是不是太过分了?我最好只是将代码添加到事件处理程序来显示对话框吗?
I'm building a WPF app and trying to conform to MVVM best practices. I'm using the MVVM Foundation framework and noticed the Messenger class, which I've read should be used for handling dialogs in WPF. This sounds great, but I'm totally not understanding how to use a Messenger for this purpose. Literally, all I want to do is open a modal About dialog --I don't need to pass any messages back and forth.
Was the intent of the Messenger class to be used for cases where dialogs require a message from its parent, or return a message to its parent? Is it overkill for an About dialog? Would I be better off simply adding code to an event handler to show the dialog?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
消息传递模式背后的想法与显示对话框没有任何关系。这个想法只是提供一种在 ViewModel 之间进行通信的解耦方式。
您可以利用此基础设施来解决您的问题,但您必须自己实现对话框的显示。
正如 Phillip 上面所示,您可以在 ViewModel 之间发送消息。当您的 ViewModel 收到消息时,它可以将其自己的内部属性(例如“ShowDialog”)设置为 true。
然后,您可以拥有一个对此属性更改操作做出反应并打开一个对话框的绑定。
我还为 MVVM 模式构建了一个简单的消息传递框架,该框架借鉴了 Josh 的想法(以及其他几个现有框架),您可以阅读它 此处
The idea behind the messaging pattern doesn't specifically have anything to do with showing dialogs. The idea is simply to provide a decoupled way to communicate between ViewModels.
You can leverage this infrastructure to solve your problem but you will have to implement the showing of the dialog yourself.
As Phillip showed above you can send messages between ViewModels. When your ViewModel receives the message it can set it's own internal property, say "ShowDialog", to true.
You can then have a binding that reacts to this property change operation and opens a dialog.
I have also built a simple messaging framework for the MVVM pattern that borrows from Josh's idea (and several other existing frameworks) you can read about it here
假设您有一个父视图和一个对话框视图。在 MVVM 中,它们都有一个视图模型。最好保持这些视图模型解耦,即它们彼此之间没有引用。然而他们需要相互沟通。 Messenger 类充当中间人或中介者,以协调两个类之间的信息通信。请参阅Josh 的博客。
这是对象 A。它是对中介者的调用Register方法实现:当我从中介收到消息ObjectBSaidSomething时,我会将其缓存在成员WhatObjectBSays中。
这是对象 B,它实现: I'我要发送一条消息 ObjectBSaidSomething。请注意,对象 B 对对象 A 一无所知。可能没有任何对象侦听 ObjectBSaidSomething,或者有 100 个对象侦听 ObjectBSaidSomething,但对象 B 不知道也不关心。这是很好的解耦,这就是中介者模式是个好主意的原因。这就是 MVVM 基金会建议在视图模型之间传递信息的方式。
Say you have a parent view and a dialog view. In MVVM they would both have a view model. It is good to keep these view models decoupled, i.e. they don't have references to each other. And yet they need to communicate to each other. The Messenger class acts as a go between or Mediator to mediate the communication of information between the two classes. See the code taken from Josh's blog.
Here is Object A. It's call to the mediator's Register method implements: when I receive the message ObjectBSaidSomething, from the mediator, I'll cache it in the member WhatObjectBSays.
Here is Object B, which implements: I'm going to send a message ObjectBSaidSomething. Note, that Object B knows nothing about Object A. There might be nothing listening for ObjectBSaidSomething, or 100 objects listening for ObjectBSaidSomething, but Object B doesn't know and doesn't care. This is good decoupling, and this is why the Mediator pattern is a good idea. And this is the way the MVVM foundation is recommending that information is passed between view models.