Leveraging x:Bind with events
In the previous chapter, we bound ViewModel
commands to the Command
properties of the Add Item and Delete Item buttons. This works great and keeps the ViewModel decoupled from the UI, but what happens if you need to handle an event that isn’t exposed through a Command
property? For this scenario, you have two options:
- Use a custom behavior such as
EventToCommandBehavior
in the .NET MAUI Community Toolkit. This allows you to wire up a command in the ViewModel to any event. - Use
x:Bind
in the view to bind directly to an event handler on the view model.
In this application, we will use x:Bind
. This option will provide compile-time type checking and added performance. If you want to learn more about the .NET MAUI Community Toolkit, you can read the documentation on Microsoft Learn: https://learn.microsoft.com/dotnet/communitytoolkit/maui/behaviors/event-to-command-behavior.
We want to provide users of the My Media Collection application...