WPF Commands vs Events Advantages/Disadvantages
Asked Answered
wpf
N

5

52

Can anyone tell me what are the advantages of using Commands vs Events in WPF. Do Commands or Events run into memory leaks? What is the fastest approach. What are their disadvantages.

Naif answered 1/6, 2010 at 20:8 Comment(0)
B
41

Commands provide two main benefits over event handlers:

  1. commands are not linked to the caller, so same command is not dependent and can be called from menu item, toolbar button, keyboard, etc.
  2. commands provide support for enabling/disabling all related UI controls based on the status of command (can be executed or not)

I'd prefer using commands at real project, especially if you want to use M-V-VM.

I haven't heard about any memory leaks related with commands.

Events are probably faster, but the difference should not be significant - I've been using commands on my projects for 2 years and hadn't any performance issues with them.

For more details on commands see Commanding Overview(archive)(v4)

Brumfield answered 1/6, 2010 at 20:25 Comment(2)
Can't you subscribe the same event handler to a toolbar button, keyboard, etc? These seem like weak reasons for adding a whole layer of complexity to your code tbh.Prepossession
A better argument would be that event handlers can be managed only in code behind, you can’t simply move it to a ViewModel. Commands offer loose coupling.Prepossession
B
37

But although Commands and Events can be overlapping, they are two different things. Commands say "do this!", while events say "this just happened!". So you might have a CloseWindowCommand for closing a window, but the window might have a ClosingEvent that tells subscribing objects that is is closing.

Balling answered 24/11, 2010 at 12:57 Comment(2)
@Shoe - What else did you expect? This totally makes sense and is not a contrary to what T.J.Kjaer said.Mantilla
@Wolle The English of saying "this just happened" before "do this!" is what I was remarking about. I never said he was wrong.Eustacia
E
5

Commands are a more standard way to integrate events. the can be more usefull than events because with the help of them you can define a single task (command) and use it from different places. for example you can define a save command and use a menu item, a context menu item and a button to use it at the same time. this way you can centerlize the tasks. also commands support data-binding which is a very powerfull feature of WPF application. as far as I know, commands lead to certain kinds of memory leaks but you can avoid that by using many work arounds. I must add that MVVM design pattern also uses commands as a standard way to design WPF application. working with events is much simpler but commands provide much powerfull design. but you must now that you can't always use commands instead of events. there any many places that you can only use events.

Elicia answered 1/6, 2010 at 20:26 Comment(0)
K
0

Additionally WPF4.0 allows binding to command definitions. This makes it even easier to expose commands from your view models, utlimately helping you in separating logic from UI concerns.

Kelsy answered 1/6, 2010 at 20:32 Comment(1)
Can you elaborate?Monaural
Q
-6

Commands are routed events.

Qp answered 17/8, 2010 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.