I am wondering how to achive clean architecture in my solution. I have projects: Core (Models, Messages, ViewModels), Mobile (Maui project -> Views), Shared (Dto project). I have:
<PanGestureRecognizer PanUpdated="PanGestureRecognizer_PanUpdated"/>
in my View, which I pass it to the MyViewModel
via:
private void PanGestureRecognizer_PanUpdated(object sender, PanUpdatedEventArgs e)
{
var panUpdatedCommand = ((MyViewModel)BindingContext).PanUpdatedCommand;
panUpdatedCommand.Execute(e);
}
Here I pass the EventArgs: PanUpdatedEventArgs
which depends on Maui.
Then I can't use RelayCommand
in MyViewModel
like this:
[RelayCommand]
private void PanUpdated(PanUpdatedEventArgs e)
{
switch (e.StatusType)
{
case GestureStatus.Started:
...
case GestureStatus.Running:
...
case GestureStatus.Canceled:
...
case GestureStatus.Completed:
...
}
}
because PanUpdatedEventArgs
and GestureStatus
come from Maui library but my MyViewModel
should be independent due to clean architecture. How can I achive that? Or should I put my ViewModel folder in Mobile layer?
You add CommunityToolkit MAUI, and make use of EventToCommandBehavior.
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/behaviors/event-to-command-behavior
Two important things:
EventArgsConverter
to convert the EventArgs to another type that is passed to the command.This is the way I use events in general.
(The next is my opinion) I seriously doubt that this event has any reason to turn into command. This seems like a mechanical code, that if anywhere - belongs to the View itself.