在MVVM中WPF命令的接受模式是什么? [英] What is the accepted pattern for WPF commanding in MVVM?

查看:225
本文介绍了在MVVM中WPF命令的接受模式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个WPF应用程序,我理解命令模式很好,但我发现有几个不同的实现的命令模式的MVVM。在他的WPF示例应用程序中有Josh Smith的实现,来自Prism的 DelegateCommand CommandBindings 实现

I'm working on a WPF app and I understand the command pattern pretty well, but I've found that there are several different implementations of the command pattern for MVVM. There's Josh Smith's implementation in his WPF sample app, the DelegateCommand from Prism, and the CommandBindings implementation.

我的问题是,使用MVVM的命令是什么普遍接受的最佳实践?我的应用程序使用Prism,因此 DelegateCommand 可供我们使用。

My question is, what is the generally accepted best practice for using commands with MVVM? My application uses Prism so DelegateCommand is available to us.

我的团队的开发人员正在争论哪种方法最好。一些不喜欢为每个命令生成的众多.cs文件,另一些人喜欢一切都通过 CommandBindings 连接。我在失去。

The devs on my team are arguing about which approach is "best." Some don't like the numerous .cs files generated for each command, others prefer that everything be wired up via CommandBindings. I'm at a loss. Can anyone shed some light?

推荐答案

有两点需要考虑:

不同的MVVM框架提供的命令,例如CommandSinkCommand或SimpleCommand(来自Cinch)等,作为正常的ICommands工作。特别是,当WPF相信发生了可能导致UI更改的事情时,将评估CanExecute处理程序。您也可以通过 CommandManager.InvalidateRequerySuggested()手动强制此操作。

The commands provided by different MVVM frameworks, such as the CommandSinkCommand or SimpleCommand (from Cinch), etc. work as normal ICommands do. In particular, the CanExecute handler is evaluated whenever the WPF believes something has happened which could cause an UI change. You can also manually force this through CommandManager.InvalidateRequerySuggested().

与此相反,DelegateCommands&的Prism不调用CanExecute处理程序,除非您手动调用 RaiseCanExecuteChanged()命令。如果你想改变它,所以当命令正常请求时也会被触发,请参阅 http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=47338

In contrast to that, the DelegateCommands<> of Prism do not call the CanExecute handler unless you manually call RaiseCanExecuteChanged() on the command. If you wish to change this so it also gets fired when the commands are normally requeried, see the code at http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=47338

编辑:

第二点:MVVM框架的命令通常接受一个对象作为命令参数。相比之下,Prism的DelegateCommands<>更强类型(虽然你当然可以有一个DelegateCommands如果你想)。

Second point: The commands of MVVM frameworks usually accept an object as command parameter. In contrast, the DelegateCommands<> of Prism are more strongly typed (though you could of course have a DelegateCommands if you want to).

这篇关于在MVVM中WPF命令的接受模式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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