Xamarin表单-自定义ViewCell的图像点击处理程序
我有一个自定义单元格的列表视图。在每个自定义单元格中,用户可以点击一个椭圆形图像来调用单元格中的功能。在这一点上,我关注的是一个美学问题 注意,我用一个包含带椭圆的图像的按钮实现了这一点。我使用了一个按钮,因为它本机响应点击事件。问题是按钮有边框。此外,该按钮还有一个不太理想的点击动画Xamarin表单-自定义ViewCell的图像点击处理程序,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一个自定义单元格的列表视图。在每个自定义单元格中,用户可以点击一个椭圆形图像来调用单元格中的功能。在这一点上,我关注的是一个美学问题 注意,我用一个包含带椭圆的图像的按钮实现了这一点。我使用了一个按钮,因为它本机响应点击事件。问题是按钮有边框。此外,该按钮还有一个不太理想的点击动画 <ListView x:Name="___listview" HasUnevenRows="True"> <ListView.ItemTemplate> <
<ListView x:Name="___listview" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="10" Margin="10">
<Button Image="{Binding ImageName}" Command="{Binding UpCount}"
BackgroundColor="White" WidthRequest="50" />
<Label Text="{Binding Count}" HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我认为用一个简单的图像代替按钮会更好。但是,图像没有本机的“点击”处理或ICommand处理。使用tappesturerecognizer
可以将此类行为附加到包括图像在内的其他元素。代码看起来像这样
var tg= new TapGestureRecognizer();
tg.Tapped += (s, e) => {
// handle the tap
};
image.GestureRecognizers.Add(tg);
每个ViewCell都有一个该手势识别器需要附加的图像。但是,我无法在模型中按名称引用图像
是否有方法为每个ViewCell的图像启用tap处理程序并在模型中处理tap(非代码隐藏)?是的,您可以在Xaml中添加TapGestureRecognitor并在Viewmodel中接收命令。有关更多信息,请参阅 在你的情况下,它看起来像这样
<Image Source="{Binding ImageName}">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding UpCount}" />
</Image.GestureRecognizers>
</Image>
<Image Source="{Binding ImageName}">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding UpCount}" />
</Image.GestureRecognizers>
</Image>