XAML > ViewModel 命令参数 - 无法从对象转换为布尔值?

问题描述 投票:0回答:2

我有这个XAML

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

以及我的 ViewModel 中的这段代码

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

我试图从 XAML 传递参数 true 或 false,但在命令 C# 代码中,val 下有一行显示“无法从对象转换为 bool”。谁能帮我解决这个问题吗?

xamarin xamarin.forms
2个回答
4
投票

修改如下,我们可以通过TapGestureRecognizer.CommandParameter里面的x:type定义我们想要的类型

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

0
投票
你可以使用这个方法

© www.soinside.com 2019 - 2024. All rights reserved.