如何在 Xamarin 中处理 FlexLayout 的触摸事件 [英] How to handle touch event for a FlexLayout in Xamarin
问题描述
所以我构建了一个应用程序,我应该在其中显示多个产品,因为我想在一行中显示多个产品,我不能使用 ListView,所以我考虑使用 FlexLayout 作为 Bindable-Layout 和使用 ItemsSource 显示我的产品列表,这是成功的.所以我想为 flexlayout 中显示的每个产品添加一个触摸事件,为此我创建了一个新的行为,这是我使用的代码的链接:
So Im building an app where I should show multiple products, since I want to show more than just one product in one row, i couldn't use a ListView, so I thought about using a FlexLayout as a Bindable-Layout and use the ItemsSource to display my list of products, whiche was a sucess. So I wanted to add a touch event to each of my products shown in the flexlayout, to do so I created a new Behaviour, this is the link for the code I used :
https://gist.github.com/jtaubensee/96a5e49c66a2027e36p>f1d2114d
https://gist.github.com/jtaubensee/96a5e49c66a205e36ff32787f1d2114d
确实有效,因此我可以使用命令.我的问题是我想获得被点击的产品,但我不知道该怎么做?有没有人可以帮我解决这个问题?
that did work and therefor I can use a Command. my problem is that I want to get the product that was clicked, and I can't figure out how to do ? is there anyone who can possibly help me with that ?
推荐答案
如果你不反对 xaml,我就是这样处理的.
If you are not adverse to xaml, this is how I handle it.
<FlexLayout BindableLayout.ItemsSource="{Binding Abilities}" IsVisible="{Binding HasAbilities}" BindableLayout.ItemTemplate="{DataTemplate attitm:AttachedAbility}"
AlignItems="Center" Wrap="Wrap" JustifyContent="Center"/>
并且模板实现了触摸手势,并将对象作为命令参数传递;
and the template implements the touch gesture, and passes the object as the Command Parameter;
<ContentView.Content>
<StackLayout Padding="20,8" HorizontalOptions="Center">
<Frame BorderColor="{OnPlatform Android=DarkCyan, UWP=Accent}" Padding="4">
<Frame.GestureRecognizers>
<TapGestureRecognizer Command="{Binding DrillIntoCommand}" CommandParameter="{Binding}"/>
</Frame.GestureRecognizers>
<StackLayout HorizontalOptions="Center" Orientation="Horizontal">
<Label x:Name="TitleLabel" Text="{Binding Title}" HorizontalOptions="Center" VerticalOptions="Center"/>
<Label Text="" FontFamily="{StaticResource FontAwesomeSolid}" IsVisible="{Binding IsUserCreated}" TextColor="Orange" HorizontalOptions="Center"/>
</StackLayout>
</Frame>
</StackLayout>
</ContentView.Content>
这篇关于如何在 Xamarin 中处理 FlexLayout 的触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!