使用MVVM Foundation Messenger显示对话框 [英] Using MVVM Foundation Messenger to Show Dialog

查看:211
本文介绍了使用MVVM Foundation Messenger显示对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个WPF应用程序,并尝试符合MVVM最佳实践。我正在使用 MVVM Foundation 框架,并注意到 Messenger 类,我读过的应该用于< WPF中的href =http://stackoverflow.com/questions/454868/handling-dialogs-in-wpf-with-mvvm>处理对话框。这听起来不错,但是我完全不了解如何使用Messenger来实现此目的。从字面上来说,我想做的只是打开一个模态关于对话框 - 我不需要来回传递任何消息。



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆