Home c# What's the difference between teams from the event handlers?

What’s the difference between teams from the event handlers?

Author

Date

Category

In WPF and MVVM there is a clear paradigm used commands inherit the ICommand, separating logic from presentation, etc. However, after going to the event model in the MVVM is not clear what their differences from the event handlers?


Answer 1, Authority 100%

I will try in my own words, as he has recently got off the WinForms-approach MVVM and happy about it. My amateur opinion, the genius not claim.

Events

  • The event handler receives a reference to a control in the sender and all the necessary information about the event in the EventArgs . Then you can do with them what is required: to change the behavior of the control, or to send the data as far as possible. Set the input data is fixed and not kastomiziruetsya.
  • The event is called when we need to, the key is pressed, move the mouse, and so on. There are many different events that can come from almost any kind of problems.

but

  • Are all in one class
  • It is difficult to test

Team

  • be anywhere where you can refer to in the declaration Binding , the default there, which refers to DataContext .
  • can pass any argument in the command.
  • The command can be called when necessary through a direct connection, such as a button or via KeyBindings .
  • Are Allowed to perform the command, you can find out in advance of its implementation, the condition is placed in the CanExecute . However, many controls have a built-in response to CanExecute, and like yourself can transfer his fortune in Disabled , if CanExecute returns false (eg MenuItem or Button ). It’s convenient.
  • It is easy to test by simply calling the command separately from the test method.

Team is not always necessary, for example, they do not need, if we want to do something, if kontrola changing some property. In this case, we can simply bind the property itself and execute code on the fact of changing the properties. The same team do not need to change the appearance or behavior control. In 99% of cases, this is solved directly in xaml with styles and templates.

Currently, the disadvantages have I do not see teams just their purpose quite different, is not such as in event handlers, I would even compare did not.

The most convenient way to use commands, in my opinion, best described by here under Relaying Command Logic .


Answer 2, Authority 67%

I think that the main advantage of the teams – the separation of logic and presentation (what you say). A little clarification:

  • You create a team, and you can use it in many View , many times. The event is closely linked to the source, so the handler is hung up on the View , on which hangs the event;
  • This leads to the fact that the team is easier to test;
  • The teams have two methods: Execute and CanExecute . Very convenient that in CanExecute , you can specify a condition when the load command, and when not to.

Also, you can read those two links (they though in English):

1) https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-3.5/ms752308 (v = vs.90)? redirectedfrom = MSDN

2) https://stackoverflow.com/questions/8452/custom-wpf-command -pattern-example

In addition, you can add that in wpf realized many built-in commands. Here is their list: https://metanit.com/sharp/wpf/7.1.php

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions