I was just googling the difference between WPF Command and Event in WPF. i landed on the following page of * where the discussion is going on.
我刚刚搜索了WPF命令和WPF事件之间的区别。我登陆了*的下一页,讨论就在这里进行。
What is the difference between WPF Command and Event?
WPF命令和事件之间有什么区别?
I am only able to understand following from there
我只能从这里理解以下内容
- Commands can be written in business layer while event only in presentation
- 命令可以在业务层中编写,而事件仅在表示时进行。
- 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?
我说的对吗?他们之间还有什么不同吗?
2 个解决方案
#1
5
You're right but only partly.
你说得对,但只是部分正确。
- 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. - 主要的问题是事件不适合MVVM范式。简单地说事件很难插拔,您不能绑定到事件处理程序。尽管如此,在表示层中使用命令没有限制,但是这样做没有任何好处。您也可以在BLL中捕获您的控件并将事件处理程序附加到它,但是这种情况更糟糕。
- No. Event handlers also could be associated to different controls as long as event args type match.
- 不。只要事件args类型匹配,事件处理程序也可以关联到不同的控件。
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.
同时,命令也给你一些免费的好处。例如,如果CanExecute()返回false,将禁用使用命令元素。另一个好处是,使用命令可以迫使您遵循关注点分离原则。
#2
4
An Event is a trigger that occurs when something happens in the UI. A Command is how this event is handled by your domain model.
事件是在UI中发生事情时发生的触发器。命令是域模型处理此事件的方式。
#1
5
You're right but only partly.
你说得对,但只是部分正确。
- 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. - 主要的问题是事件不适合MVVM范式。简单地说事件很难插拔,您不能绑定到事件处理程序。尽管如此,在表示层中使用命令没有限制,但是这样做没有任何好处。您也可以在BLL中捕获您的控件并将事件处理程序附加到它,但是这种情况更糟糕。
- No. Event handlers also could be associated to different controls as long as event args type match.
- 不。只要事件args类型匹配,事件处理程序也可以关联到不同的控件。
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.
同时,命令也给你一些免费的好处。例如,如果CanExecute()返回false,将禁用使用命令元素。另一个好处是,使用命令可以迫使您遵循关注点分离原则。
#2
4
An Event is a trigger that occurs when something happens in the UI. A Command is how this event is handled by your domain model.
事件是在UI中发生事情时发生的触发器。命令是域模型处理此事件的方式。