点击 ListView 中的图像,我们应该得到特定行的 属性 值

Tap on a image inside the ListView, we should get a property value of specific Row

我有一个 listView,它包含一个带有项目列表的图像。当我点击图像时,我应该获得该行的 ProfileID。

<Image Source="{Binding ImageUrl}" x:Name="{Binding ProfileID}" Aspect="AspectFill">
    <Image.GestureRecognizers>
    <TapGestureRecognizer Tapped="imageUserGesture_Tapped"/>
    </Image.GestureRecognizers>
</Image>

private async void imageUserGesture_Tapped(object sender, EventArgs e)
{
    CloseAnimation();
    var img = ((Image)sender);
    var name = img.Name;//How can I read name property ??????
    //var name = e.LoadFromXaml(MatchProfile).Name;
}

Name 不是您可以参考的实际 属性,它只是一个 XAML 工件(因此有“x”前缀)。相反,您可以使用 BindingContext 来获取您需要的数据

var img = ((Image)sender);
var context = (MyClassGoesHere)img.BindingContext;
var name = context.ProfileID;
<Image Source="{Binding ImageUrl}"
       x:Name="imageUser"
       Aspect="AspectFill">
                                                                
   <Image.GestureRecognizers>
      <TapGestureRecognizer Tapped="imageUserGesture_Tapped"
         Command="{Binding TapCommand}"
         CommandParameter="{Binding ProfileID}"
         NumberOfTapsRequired="1"/>
                                                                
   </Image.GestureRecognizers>

</Image>