MVVM 和显示更多 GUI 的命令 [英] MVVM and commands that show more GUI

查看:17
本文介绍了MVVM 和显示更多 GUI 的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 RelayCommand 的 MVVM 想法视图模型.对于无需进一步用户输入即可完成的操作,这很好也很优雅.简单的.可测试.

I like the MVVM idea of a RelayCommand that's exposed by the ViewModel. That's nice and elegant, for operations that can be done with no further user input. Simple. Testable.

然而,并非所有操作都没有 UI.有些需要确认(您确定要删除吗?").其他人还需要更多信息.打开文件可能需要从文件打开对话框到成熟的导入向导.

However, not all operations are UI-less. Some require confirmation ("Are you sure you want to delete?"). Others require more information yet. Opening a file might entail anything from a File Open dialog up through a full-blown Import wizard.

在 MVVM 应用程序中,编写需要用户输入的命令的最佳方法是什么?是否有一种既定的模式可以通过依赖注入以某种方式解决这个问题?我应该在代码隐藏中编写一个 KeyDown 处理程序,并让它显式执行事件吗?我是否应该使用 RoutedUICommand,并将所有显示下一个 GUI"代码放在我的视图中?或者有什么明显的东西我完全失踪了?

What's the best way, within an MVVM app, to write commands that require user input? Is there an established pattern for solving this with dependency injection somehow? Should I write a KeyDown handler in the code-behind, and have it explicitly execute the event? Should I fall back on RoutedUICommand, and put all the "display the next GUI" code in my View? Or is there something obvious that I'm totally missing?

推荐答案

我通常使用依赖注入来注入某种抽象的 IShowTheInterface 东西,然后从 Command 内部调用抽象上的方法.然后,这些方法应该会为您提供确定是否继续执行该操作以及用户提供的输入所需的答案.

I usually use Dependency Injection to inject some kind of abstract IShowTheInterface thing, and then invoke methods on the abstraction from within the Command. These methods should then give you the answers you need to determine whether to continue with the action at all, and what input the user gave.

我最近在博客文章中使用了这个例子不同的主题.

这篇关于MVVM 和显示更多 GUI 的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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