Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Command (ICommand) and Click event

When should I use the Command and when to use the Click event?

F.e. if I have a Button in my UWP app what should I use?

like image 855
Marian Dolinský Avatar asked Sep 18 '16 09:09

Marian Dolinský


People also ask

What is the use of ICommand in WPF?

Commands provide a mechanism for the view to update the model in the MVVM architecture. Commands provide a way to search the element tree for a command handler.

What is ICommand in MVVM?

ICommand is an interface between the Presentation & the BusinessLogic layer. Whenever any button is pressed on the screen, XAML has its code-behind ButtonClick event. But in MVVM architecture, there is no room for code-behind to keep the application loosely coupled, so ICommand was introduced.

What is ICommand in c#?

The ICommand interface is the code contract for commands that are written in . NET for Windows Runtime apps. These commands provide the commanding behavior for UI elements such as a Windows Runtime XAML Button and in particular an AppBarButton .

Why should we use routed commands instead of events?

Routed commands give you three main things on top of normal event handling: Routed command source elements (invokers) can be decoupled from command targets (handlers)—they do not need direct references to one another, as they would if they were linked by an event handler.


1 Answers

When should I use the Command and when to use the Click event?

Yours is a broad question and I would simply answer with: "It depends". Because:

  • The Command implements the ICommand interface and this means more code to add to your application but usually this won't change. Instead, the event handler doesn't require any interface implementation.
  • For every command you want, you have to provide the code that will handle the click and the CanExecute logic, to say when the command can execute. This is not requested in a simple event handler (like MyButton_Click). This means that, using a Command, you will have more control over the elements of your UI (the button won't execute anything if CanExecute is false).
  • When you want to add a Command, you will bind it to your DataContext (the ViewModel, if you implement the MVVM pattern). Instead, when you add a simple event handler (like MyButton_Click), the code will be placed in your code-behind that is the logic behind your main window. This means that implementing a Command, according to me, you'll have everything you need to modify in just one place (the ViewModel) instead of logic scattered everywhere in your project.

Of course, you can use whatever you want and my points are there just to give you an insight about these different implementations and you have to consider which solution is suitable for you, considering also the requirements you have been given (like: "Don't use event handlers" or "The Command is too advanced, let's just use something simple", etc.) and/or other constraints in your project.

like image 77
Alberto Solano Avatar answered Sep 27 '22 20:09

Alberto Solano