在 WPF MVVM 中打开对话框 [英] Open dialog in WPF MVVM

查看:53
本文介绍了在 WPF MVVM 中打开对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要从用户输入一些信息的按钮打开一个对话框.

I have an application that need to open a dialog from a button where the user enters some information.

目前我是这样做的(效果很好)

At the moment I do it like this (which works fine)

  • 单击按钮会在 ViewModel 中生成一个命令.
  • ViewModel 引发控制器监听的事件.
  • 控制器计算出新窗口的细节(即视图、视图模型和模型)并打开它(ShowDialog)
  • 当窗口关闭时,Controller 将结果添加到 eventargs 并返回到 ViewModel
  • ViewModel 将信息传递给 Model.

有很多步骤,但它们都有意义,而且打字不多.

There are a lot of steps but they all make sense and there is not much typing.

代码如下(窗口要求输入用户名)

The code looks like this (the window asks for the user's name)

视图模型:

AskUserNameCommand = DelegateCommand(AskUserNameExecute);
...

public event EventHandler<AskUserEventArgs> AskUserName;

void AskUserNameExecute(object arg) {
    var e = new AskUserNameEventArgs();
    AskUserName(this, e);
    mModel.SetUserName(e.UserName);
}

控制器:

mViewModel.AskUserName += (sender,e) => {
    var view = container.Resolve<IAskUserNameView>();
    var model = container.Resolve<IAskUserNameModel>();
    var viewmodel = container.Resolve<IAskUserNameViewModel>(view, model);
    if (dlg.ShowDialog() ?? false)
        e.UserName = model.UserName;
}

我的问题是水平通信在 MVVM 模式中是如何工作的.不知何故,让控制器参与模型之间的数据传输似乎是错误的.

My question is how the horizontal communication works in the MVVM pattern. Somehow it seems wrong to let the controller be involved in the data transfer between the models.

我查看了中介模式,让模型直接进行通信.不喜欢这个想法,因为它使模型依赖于 GUI 的实现细节.(即如果对话框被替换为文本框,则模型需要更改)

I have looked at the mediator pattern to let the models communicate directly. Don't like that idea since it makes the model depending on implemetations details of the GUI. (i.e. if the dialog is replaced with a textbox, the model need to change)

推荐答案

出于某种原因,我不喜欢当前的大多数建议,所以我想我会链接到一个几乎相同的问题,并附上我的答案喜欢:

I don't like most of the current suggestions for one reason or another, so I thought I would link to a nearly identical question with answers I do like:

打开文件对话框MVVM

具体来说,Cameron MacFarland 的回答正是我所做的.通过界面提供的服务来提供 IO 和/或用户交互是这里的最佳选择,原因如下:

Specifically the answer by Cameron MacFarland is exactly what I do. A service provided via an interface to provide IO and/or user interaction is the way to go here, for the following reasons:

  • 可测试
  • 它抽象出任何对话框的实现,以便您可以更改处理这些类型事物的策略,而不会影响组成代码
  • 不依赖任何沟通模式.您在那里看到的许多建议都依赖于中介,例如事件聚合器.这些解决方案依赖于与调解人另一端的合作伙伴实施双向通信,这既难以实施,又是一个非常松散的合同.
  • ViewModel 保持自治.我和你一样,觉得控制器和 ViewModel 之间的通信不对.如果没有其他原因可以简化可测试性,ViewModel 应该保持自治.
  • It is testable
  • It abstracts away the implementation of any dialogs so that your strategy for handling these types of things can be changed without affecting constituent code
  • Does not rely on any communication patterns. A lot of suggestions you see out there rely on a mediator, like the Event Aggregator. These solutions rely on implementing two-way communication with partners on the other side of the mediator, which is both hard to implement and a very loose contract.
  • ViewModels remain autonomous. I, like you, don't feel right given communication between the controller and the ViewModel. The ViewModel should remain autonomous if for no other reason that this eases testability.

希望这会有所帮助.

这篇关于在 WPF MVVM 中打开对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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