【问题标题】:Xamarin Forms TapGestureRecognizer blocks actual tappingXamarin Forms TapGestureRecognizer 阻止实际点击
【发布时间】:2021-12-06 10:18:50
【问题描述】:

我在 collectionview 中有 stacklayout,我想点击 stacklayout 元素来执行绑定命令。 我将 TapGestureRecognizer 添加到 stacklayout 的那一刻 - Stacklayout 停止响应触摸(我仍然可以滚动它)并且绑定命令不会触发:

<CollectionView ItemsSource="{Binding ResponseResponses}" SelectionMode="Single" >
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <StackLayout>
                                <StackLayout.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding SearchCommand}" NumberOfTapsRequired="1"/>
                                </StackLayout.GestureRecognizers>
                                <Label Text="{Binding Username, StringFormat=User: {0}}" />
                                <Label Text="{Binding FileCount, StringFormat=Files: {0}}" />
                                <BoxView   HeightRequest="1"
                                           BackgroundColor="LightGray"
                                           VerticalOptions="End"/>      
                            </StackLayout>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

因此,如果没有 GestureRecognizers - 对 stacklayout 元素的选择有效,使用 - 则无效。 如果我将 GestureRecognizers 添加到标签(例如),则触摸会在该标签上被阻止。

顺便说一句,{Binding SearchCommand} 是自己工作的,它绑定到视图中的其他按钮,所以可能不是原因。

那么,我在这里做错了什么?

【问题讨论】:

  • 另外刚刚发现正是 标签阻止了触摸责任。为空或有绑定无关紧要
  • SearchCommand 是做什么的?
  • 从 viewmodel 运行异步函数(顺便说一句,我发现了问题所在,误解了默认绑定的内容)

标签: c# .net xamarin xamarin.forms xamarin.android


【解决方案1】:

所以,正如我在这里找到的: CollectionView Tips

请记住,在 ItemTemplate 中工作时,BindingContext 是项目本身,而不是 ContentPage 的项目。如果您的命令存在于该项目上,那么您就完成了。但是,如果您希望将此绑定路由到 ContentPage 的视图模型上的命令,如本例所示,您可以使用新支持的 RelativeSource。

所以你必须使用这样的绑定:

<CollectionView.ItemTemplate>
    <DataTemplate>
        <views:ResultViewA>
            <views:ResultViewA.GestureRecognizers>
                <TapGestureRecognizer
                    Command="{Binding 
                        Source={RelativeSource 
                        AncestorType={x:Type vm:FlightResultsViewModel}}, 
                            Path=GoToDetailsCommand}"
                    CommandParameter="{Binding .}"/>
            </views:ResultViewA.GestureRecognizers>
        </views:ResultViewA>
    </DataTemplate>
</CollectionView.ItemTemplate>

就我而言,它现在看起来像这样:

<StackLayout>
       <StackLayout.GestureRecognizers>
                <TapGestureRecognizer
                       Command="{Binding 
                       Source={RelativeSource 
                       AncestorType={x:Type viewmodels:SearchViewModel}}, 
                       Path=SearchCommand}"
                       CommandParameter="{Binding .}"/>
       </StackLayout.GestureRecognizers>
       <Label Text="{Binding Username, StringFormat=User: {0}}"/>
       <Label Text="{Binding FileCount, StringFormat=Files: {0}}"/>
       <BoxView   HeightRequest="1"
                  BackgroundColor="LightGray"
                  VerticalOptions="End" InputTransparent="True"/>

【讨论】:

    猜你喜欢
    • 2020-01-15
    • 2022-01-15
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多