【发布时间】:2018-02-06 15:41:44
【问题描述】:
我有一个带有 ViewCell 的 Listview,在这个 ViewCell 中有 3 个图像和 3 个标签。为了处理点击事件,我为每个控件编写了一个 GestureRecognizers。
<ListView HasUnevenRows="True"
SeparatorColor="White"
ItemsSource="{Binding List}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
ItemSelected="Handle_ItemSelected"
ItemTapped="ListView_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Spacing="0">
<StackLayout Orientation="Horizontal" Padding="16,8,16,4">
<Image Source="{Binding IconImageSource}" HeightRequest="36" WidthRequest="36"/>
<StackLayout HorizontalOptions="StartAndExpand" Padding="8,0,0,0" Spacing="0">
<Label Text="{Binding LocalizedText}" TextColor="White"/>
<Label Text="{Binding Value}" TextColor="Gray" FontSize="Small"/>
</StackLayout>
</StackLayout>
<BoxView Color="WhiteSmoke" HeightRequest="1" HorizontalOptions="FillAndExpand" Opacity="0,5" />
<RelativeLayout Padding="0,4,0,4" >
<Image Source="ic_ecu_eye"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.25}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=4}"
/>
<Label Text="{Binding ViewedCount}"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.25, Constant=20}" />
<Image Source="ic_ecu_favorite"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=4}"/>
<Label Text="{Binding FavoriteCount}"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5, Constant=20}" />
<Image Source="ic_ecu_comment"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.75}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0, Constant=4}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
<Label Text="{Binding CommentsCount}"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.75, Constant=20}">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1"/>
</Label.GestureRecognizers>
</Label>
</RelativeLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
代码隐藏
async private void TapGestureRecognizer_Tapped(object sender, System.EventArgs e)
{
// want to get listview item
}
ListView_ItemTapped 用于处理列表视图的点击事件
async private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{ // easily get the tapped item via e.Item }
与 listview 点按事件不同,TapGestureRecognizer_Tapped 无法轻松获取点按项,我需要处理一些背后的逻辑。 所以这就是我的问题,如果我在 GestureRecognizer 事件方法中的 Listview 中点击一个控件,我如何获得被点击的项目,比如这里
async private void TapGestureRecognizer_Tapped(object sender, System.EventArgs e)
{
// TODO:
}
【问题讨论】:
-
如果你想切换到
Command,你可以使用CommandParameter -
你必须直接点击你的图片。或者你可以试试带图片的按钮,也可以设置
CommandParameter