委托命令和路由命令 [英] Delegate Command and Routed Command

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

问题描述

委托命令和路由命令有什么区别?

What is the Difference between the Delegate Command and Routed Command?

我读过一些文章,说在 MVVM 上使用委托命令而不是路由命令.

I read some article that says use Delegate Command on MVVM instead of Routed Command.

那么,当我们使用 MVVM 时,Delegate Command 相对于 Routed Command 的优势是什么?

So What are the advantages of Delegate Command over Routed Command when we use MVVM?

推荐答案

使用 DelegateCommand(又名 RelayCommand)的一些优点是:

Some advantages of using a DelegateCommand (a.k.a. RelayCommand) are:

1) 需要更少的 XAML/代码来支持它们(不需要 CommandBindings)

1) Requires less XAML/code to support them (don't need CommandBindings)

2) 在 ViewModel 类中可以轻松编写命令实现代码

2) Command implementation code can easily be written in ViewModel classes

3) 它们不依赖于 UI 元素树来正常工作,这也有助于提高性能

3) They do not take a dependency on the UI element tree to work properly, which also helps improve performance

由于许多第三方 UI 控件使用路由命令,因此大多数开发人员在依赖这些控件时最终会使用路由命令.

Since a lot of third-party UI controls use routed commands, most developers end up using routed commands when taking a dependency on those controls.

如果您最终需要使用路由命令,请查看我的在 WPF 中将 RoutedCommands 与 ViewModel 一起使用 文章以了解简化事情的方法.

If you end up needing to use routed commands, check out my Using RoutedCommands with a ViewModel in WPF article to see a way to simplify things.

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

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