我有一個帶有ViewCell的Listview,在這個ViewCell中有3個圖像和3個標籤。爲了處理輕敲事件,我爲每個控件編寫了一個GestureRecognizers。如何在ListView中處理特定控件的事件(Xamarin)
<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是處理ListView的敲打事件
async private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{ // easily get the tapped item via e.Item }
與列表視圖竊聽事件,TapGestureRecognizer_Tapped不能輕易得到,我需要處理一些竊聽項目背後的邏輯。 所以這是我的問題,我怎樣才能得到輕按的項目,如果我點擊喜歡這裏的GestureRecognizer事件方法列表視圖裏的控件
async private void TapGestureRecognizer_Tapped(object sender, System.EventArgs e)
{
// TODO:
}
如果你想切換到'Command'你可以使用'CommandParameter' –
您必須直接輕擊到你的圖片,然後。或者你可以嘗試帶圖像的按鈕,也可以設置'CommandParameter' – Lee