Hello,
Firstly, please add command to the <TapGestureRecognizer Command="{Binding actionMethodCommand}" />
, not add the command to the tap event directly. And add Command in the end of actionMethod
manually.
Then, if you want to binding the viewmodel, please do not set DataType
, you can set bindingContext in the XAML like following code.
<ContentPage
...
xmlns:viewModels="clr-namespace:MyProject.Maui.ViewModels"
>
<ContentPage.BindingContext>
<viewModels:OrderDashboardViewModel />
</ContentPage.BindingContext>
<VerticalStackLayout>
<Label
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" >
<Label.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding actionMethodCommand}"
/>
</Label.GestureRecognizers>
</Label>
</VerticalStackLayout>
</ContentPage>
Here is my tested viewmodel.
public partial class OrderDashboardViewModel : ObservableObject
{
[RelayCommand]
public void actionMethod( )
{
Debug.WriteLine("Executed");
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.