.NET MAUI SideDrawer Commands
The SideDrawer control exposes a Commands
collection that allows you to
register custom commands with each control’s instance. The commands allow the user to change and extend the control default behavior.
Each element in the Commands
collection inherits from the SideDrawerCommandBase
class, and can override the CanExecute()
and Execute()
methods. Each command is associated with an event, which is represented by the command Id
property. The property needs to be set to one of the values listed below, otherwise the command service of the control will not execute the command.
-
Opening
—Executed when the side drawer is being visualized on the device screen. -
Opened
—Executed when the side drawer is already visualized on the device screen. -
Closing
—Executed when the side drawer is being hidden. -
Closed
—Executed when the side drawer is already closed.
For your convenience we have created a special SideDrawerUserCommand
class that also exposes a Command
dependency property which can be set to an instance of type that implements the ICommand
interface.
Examples
The following examples demonstrates how to use the SideDrawer commands in different scenarios.
Inheriting from the SideDrawerCommandBase class
1. Create a class which inherits from SideDrawerCommandBase
class and set the Id
property to the desired command trigger event. In addition, you can override its CanExecute()
and Execute()
methods. A sample implementation:
RadSideDrawer
and the command in XAML:
3. Add the following namespaces
Where the local
alias points to the namespace where the CustomUserCommand
is defined.
Using the SideDrawerUserCommand class
1. Define a class that implements the ICommand
interface:
2. Use this class with the SideDrawerUserCommand
in XAML like this:
Binding to a ViewModel
Use the SideDrawerUserCommand
to bind its Command
property to a view model.
1. Define the ViewModel:
2. Define the Custom Command:
3. Define the SideDrawer control:
4. Add the following namespace:
and the local
alias points to the namespace where the CustomCommand
and ViewModel
are defined.