liveBook · Manning

ICommand

As explained in .NET MAUI in Action

ICommand is a .NET interface representing an executable action together with its availability state and execution operation.

ICommand represents an executable action that a .NET MAUI control can invoke. Controls expose bindable ICommand properties, while event handlers provide a code-behind alternative. section 4.2.3

Table 4.3 Controls in .NET MAUI that initiate an action or command (view table figure)

Control

ICommand property name1

Event handler

Use case

Button

Command

Clicked

A simple control the user can tap or click to initiate an action. Uses text to convey its intent.

ImageButton

Command

Clicked

Same as Button, but uses an image to convey its intent instead of text.

SearchBar

SearchCommand

SearchButtonPressed, TextChanged (inherited from Input)

Presents a recognizable search box. It has text input and a magnifier icon. The SearchCommand ICommand and SearchButtonPressed event handlers are both linked to the magnifier icon. Any of the commands or event handlers can be linked to code that searches or filters a list.

Use the control table to choose between command-enabled controls such as Button, ImageButton, and SearchBar, and to match each command property with its user action. section 4.2.3

Commands become especially useful with MVVM. Instead of handling a CheckBox event in the page, place the action in a ViewModel and bind controls to command properties. Command supplies Execute code through a function or lambda, and a generic Command<T> can receive the affected item as a parameter. section 9.1

Commands in a todo ViewModel

A MainViewModel can expose AddTodoCommand and CompleteTodoCommand as ICommand properties. Its constructor assigns AddTodoCommand = new Command(async () => await AddNewTodo()) and CompleteTodoCommand = new Command<TodoItem>(async (item) => await CompleteTodo(item)). XAML controls then bind to these ViewModel properties, while the page's code-behind only sets its BindingContext.

How .NET MAUI in Action covers it

How deep it goes
Defines Execute and demonstrates construction, binding, asynchronous execution, and parameters, without detailing interface internals.
Examples
Uses Button, ImageButton, SearchBar, and a MauiTodo ViewModel with add and complete todo commands.
What it assumes
Assumes basic C# and .NET MAUI controls, while introducing command binding through a concrete MVVM refactoring.
What you can do after
Builds bindable actions with Command and Command<T>, including asynchronous methods and item parameters.
How it connects
Ties commands to .NET MAUI controls, data binding, MVVM, ViewModel, and INotifyPropertyChanged.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET MAUI in Action. The section links point to the parts of the book it draws on.