WPF命令与WPF中的事件有什么区别? [英] What is the difference between WPF Command and Event in WPF?

查看:1333
本文介绍了WPF命令与WPF中的事件有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在WPF中WPF命令和事件之间的区别。我着陆在下面的stackoverflow页面,讨论正在进行中。



WPF命令和事件之间有什么区别?



我只能了解以下内容


  1. 命令可以写在业务层,而事件只能在演示文稿中

  2. A单个命令可以与许多控件相关联,但事件只能与一个控件相关联。

我是对的吗?他们之间有什么区别吗?

解决方案

你是对的,但只有部分。


  1. 主要关注的是事件不适合
    MVVM 范例。简单地说,事件难以插入,你不能绑定到事件处理程序。然而,在表示层中使用命令没有限制,但没有这样做的好处。除了你可以抓住你的控制BLL并附加事件处理程序,但这种情况更糟糕。

  2. 否。只要事件参数类型匹配,事件处理程序也可以与
    不同的控件相关联。

此外,命令可以让您免费好处。例如,如果 CanExecute()返回false,则使用命令元素将被禁用。另一个好处是使用命令会迫使您遵循分离关注原则。


I was just googling the difference between WPF Command and Event in WPF. i landed on the following page of stackoverflow where the discussion is going on.

What is the difference between WPF Command and Event?

I am only able to understand following from there

  1. Commands can be written in business layer while event only in presentation
  2. A single command can be associated with many controls but event can only be associated with only one control.

Am I right? Is there any other difference between them?

解决方案

You're right but only partly.

  1. The main concern there is that events doesn't fit well into MVVM paradigm. Simply saying events are hardly pluggable, you can't bind to event handler. Nevertheless there is no limit to use commands in presentation layer but there is no benefits of doing so. As well as you could catch your control in BLL and attach event handler to it but this case is even worse.
  2. No. Event handlers also could be associated to different controls as long as event args type match.

Also commands gives you some free benefits. For example using command element'll be disabled if CanExecute() returns false. Another benefit is that using commands forces you to fo follow Separation of concerns principle.

这篇关于WPF命令与WPF中的事件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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