MVVM: commands against event handlers

I know this was done to death, but most of the answers are written like this: "Yes, you can use event handlers instead of commands if you want." This still does not solve the problem of whether writing complex commands is justified, rather than just connecting event handlers in the code behind to invoke test methods in your view model.

I don’t like commands because they produce a lot of template code and do not give me any benefit compared to the usual methods, plus some of them (for example, drag and drop) are a pain for implementation.

What is wrong with the letter:

separated code:

private void Button_Click(object sender, RoutedEventArgs e)
{
    viewModel.LoadData();
}

ViewModel:

public void LoadData()
{
   //....
}

( ), . IMO, -, . ?

+3
3

- .

, , - , . ViewModel, .

, :

  • View ViewModel. (, , ViewModel), View , ICommand View ViewModels. ( .)
  • - "-" , , " ". .
  • , . ViewModel.cs, .xaml .
  • . , .., .
+5

, . .

, . , viewmodel.

ICommand CanExecute CanExecuteChanged, / . .

, Command, , , . , , XAML.

. , .

.

+3

, . : , , .

Blend .

Another problem may occur when using event arguments when writing an event handler. If you want to switch to another control or another event, you could be very dependent on the arguments of the event, which made it difficult to switch, since the other control / event does not support the same event arguments.

0
source

Source: https://habr.com/ru/post/1792690/


All Articles