如何在Xamarin.Forms中同时长按和短按列表项?

Stu*_*rla 5 xaml xamarin.forms xamarin.forms.listview

我需要在列表中长按和短按

我正在使用效果来长按列表中的项目(ListView/CollectionView),但是当该效果起作用时,短按(点击)不起作用!

我的问题是:我是否需要创建另一个短点击效果版本,或者我可以以某种方式同时拥有两者吗?我到处搜索,没有任何信息可以帮助我找到解决方案......

我一直在我的存储库中使用此代码,但无法使两者同时工作。

<CollectionView
  x:Name="carsCollection"
  ItemsSource="{Binding Cars}"
  SelectionMode="Single"
  SelectionChangedCommand="{Binding TapCommand}"
  SelectionChangedCommandParameter="{Binding Source={x:Reference carsCollection}, Path=SelectedItem}"
  BackgroundColor="Orange">
  <CollectionView.ItemTemplate>
    <DataTemplate>
      <ContentView>
        <StackLayout
          effects:LongPressedEffect.Command="{Binding Path=BindingContext.LongTapCommand, Source={x:Reference ThisPage}}"
          effects:LongPressedEffect.CommandParameter="{Binding .}">
          <Label Text="CollectionView: Long Press works but not normal selection" />
          <StackLayout.Effects>
            <effects:LongPressedEffect />
          </StackLayout.Effects>
        </StackLayout>
      </ContentView>
    </DataTemplate>
  </CollectionView.ItemTemplate>
</CollectionView>
Run Code Online (Sandbox Code Playgroud)

包含命令的 ViewModel 位于此处

Cfu*_*fun 7

您可以使用Xamarin 社区工具包(该包收集了许多很酷的可重用/通用控件、效果、行为...)TouchEffect中的高度可自定义功能。

使用示例,您甚至可以控制长按的持续时间(默认 = 500 毫秒):

 xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
Run Code Online (Sandbox Code Playgroud)
<StackLayout
xct:TouchEffect.LongPressCommand="{Binding Path=BindingContext.LongTapCommand, Source={x:Reference ThisPage}}"
xct:TouchEffect.LongPressCommandParameter="{Binding .}"
xct:TouchEffect.LongPressDuration="2000"
xct:TouchEffect.Command="{Binding Path=BindingContext.TapCommand, Source={x:Reference ThisPage}}"
xct:TouchEffect.CommandParameter="{Binding .}">
Run Code Online (Sandbox Code Playgroud)

此外,您还可以应用动画和许多其他内容。

资源

文档(正在工作中)https://learn.microsoft.com/en-us/xamarin/community-toolkit/

仓库 https://github.com/xamarin/XamarinCommunityToolkit/

https://www.youtube.com/watch?v=BcFlZMhPmVk

  • 对于使用此功能的任何人:您无法将长按与常规手势识别器结合起来。拥有它们需要同时使用工具包 (3认同)

Wen*_*SFT 0

我的问题是:我是否需要创建另一个短点击效果版本,或者我可以以某种方式同时拥有两者吗?我到处搜索,没有任何信息可以帮助我找到解决方案......

您可以使用具有单击事件而不是堆栈布局的控件来做到这一点。

长按以下链接中的步骤。然后利用点击事件进行射门按下。 如何在 Xamarin Forms 中做出长按手势?

或者您可以使用点击手势识别器。 https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/tap