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

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

问题描述

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

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.

我最近在 blog帖子中使用了此示例一个不同的主题.

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

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