Why use commands in MVVM

前端 未结 2 653
天命终不由人
天命终不由人 2021-01-22 23:28

I\'m actually learning MVVM pattern.

I can\'t understand one thing about commands. Why use them? Why don\'t just use a function (in the View) which call a

2条回答
  •  孤街浪徒
    2021-01-23 00:03

    Another benefit from using ICommand is its bool CanExecute() method. You can monitor and control the state and define conditions when your ICommand can and will be invoked. Plus, for example, Button with bound ICommand disables itself automatically, if CanExecute() returns false (do not forget to call CanExecuteChanged() method of ICommand every time the propert(y/ies), which affect(s) CanExecute(), change(s) its/their value). By the way, the semantic of usage of this pattern is described in other answers.

提交回复
热议问题