Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML > ViewModel Command parameter - Cannot convert from object to bool?

I have this XAML

<Label.GestureRecognizers>
   <TapGestureRecognizer Command="{Binding TapGestureForUpdateCategories, Source={x:Reference MainPage}}" CommandParameter="false" />
</Label.GestureRecognizers>

and this code in my ViewModel

public Command TapGestureForUpdateCategories => new Command(val =>
{
    App.DB.UpdateAllCategoryGroups(val);
    App.DB.UpdateAllCategory(val);
});

I am trying to pass the parameter true or false from the XAML but in the command C# code there is a line under val saying "Cannot convert from object to bool". Can anyone help me with this?

like image 836
Alan2 Avatar asked Dec 03 '25 23:12

Alan2


2 Answers

Modify as below , we can define the type we want by x:type inside TapGestureRecognizer.CommandParameter

<Label.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding TapGestureForUpdateCategories, Source={x:Reference MainPage}}">
        <TapGestureRecognizer.CommandParameter>
            <x:Boolean>True</x:Boolean>
        </TapGestureRecognizer.CommandParameter>
    </TapGestureRecognizer>
</Label.GestureRecognizers>
like image 159
ColeX - MSFT Avatar answered Dec 06 '25 06:12

ColeX - MSFT


You can use this approach

<Label.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding TapGestureForUpdateCategories, Source={x:Reference MainPage}}" CommandParameter="{x:Boolean False}" />
</Label.GestureRecognizers>
like image 38
Александр Проскура Avatar answered Dec 06 '25 08:12

Александр Проскура