What is the difference between WPF Command and Event in WPF?

空扰寡人 提交于 2019-11-29 09:23:17

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/16059390/what-is-the-difference-between-wpf-command-and-event-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!