【问题标题】:How to have both long-press and short-press of list item in Xamarin.Forms?如何在 Xamarin.Forms 中同时长按和短按列表项?
【发布时间】:2020-06-26 21:46:20
【问题描述】:

我需要一个列表中的长按和短按

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

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

我一直在玩这个code in my repository,但无法同时工作。

<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>

包含命令的 ViewModel 是here

【问题讨论】:

    标签: xaml xamarin.forms xamarin.forms.listview


    【解决方案1】:

    您可以使用TouchEffect 高度可定制的Xamarin Community Toolkit 包(该包收集了许多很酷的可重用/常用控件、效果、行为...)。

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

     xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
    
    <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 .}">
    

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

    资源

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

    回购 https://github.com/xamarin/XamarinCommunityToolkit/

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

    【讨论】:

    • 对于使用此功能的任何人:您不能将长按与常规手势识别器结合使用。拥有它们都需要同时使用工具包
    【解决方案2】:

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

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

    按照以下链接中的步骤进行长按。然后使用点击事件进行压射。 How to make long press gesture in Xamarin Forms?

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

    【讨论】:

      【解决方案3】:

      在我的情况下使用 ImageButton 上的效果:

                          <CollectionView.ItemTemplate>
                              <DataTemplate x:DataType="sharedmodels:Photo">
                                  <Grid>
                                      <Grid.RowDefinitions>
                                          <RowDefinition Height="Auto"/>
                                      </Grid.RowDefinitions>
                                      <Grid.ColumnDefinitions>
                                          <ColumnDefinition Width="Auto"/>
                                      </Grid.ColumnDefinitions>
                                      <ImageButton Source="{Binding ThumbnailUrl, Converter={StaticResource ImageLocalStorageUrlConverter}}"
                                                   Command="{Binding Source={x:Reference collectionView}, Path=BindingContext.SelectCommand}" 
                                                   CommandParameter="{Binding .}"
                                                   effects:LongPressEffect.Command="{Binding Source={x:Reference collectionView}, Path=BindingContext.DeleteCommand}"
                                                   effects:LongPressEffect.CommandParameter="{Binding .}">
                                          <ImageButton.Effects>
                                              <effects:LongPressEffect/>
                                          </ImageButton.Effects>
                                      </ImageButton>
                                  </Grid>
                              </DataTemplate>
                          </CollectionView.ItemTemplate>
      

      【讨论】:

        猜你喜欢
        • 2012-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        • 2013-07-14
        相关资源
        最近更新 更多