使用MVVM Foundation Messenger显示对话框 [英] Using MVVM Foundation Messenger to Show Dialog
问题描述
Messenger类的意图用于对话需要来自其父级的消息或向其父级返回消息的情况关于对话框是否过度杀毒?我最好只是在事件处理程序中添加代码来显示对话框?
具体与显示对话框有任何关系。这个想法只是提供一种解耦的方式在ViewModels之间进行通信。
您可以利用此基础设施来解决您的问题,但您必须自己实现对话框的显示。
如上图所示,您可以在ViewModels之间发送消息。当您的ViewModel收到消息时,可以将其自己的内部属性设置为ShowDialog为true。
然后可以对此属性更改操作作出反应的绑定并打开一个对话框。
我还为MVVM模式构建了一个简单的消息传递框架,借鉴了Josh的想法(和其他几个现有的框架),您可以阅读它< a href =http://www.bradcunningham.net/labels/MVVM.html =nofollow noreferrer> here
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?
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 Foundation Messenger显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!