Xamarin Forms TapGestureRecognizer 阻止实际点击
Xamarin Forms TapGestureRecognizer blocks actual tapping
我在 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} 是独立工作的,它绑定到视图中的其他按钮,所以这可能不是原因。
那么,我做错了什么?
所以,正如我在这里发现的那样:
CollectionView Tips
Keep in mind that when working in the ItemTemplate the BindingContext is the item itself and not that of the ContentPage. If your command exists on the item, then you’re all set. However if you wish to route this binding to a command on your ContentPage‘s view model as in this example you can use the newly supported 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"/>
我在 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} 是独立工作的,它绑定到视图中的其他按钮,所以这可能不是原因。
那么,我做错了什么?
所以,正如我在这里发现的那样: CollectionView Tips
Keep in mind that when working in the ItemTemplate the BindingContext is the item itself and not that of the ContentPage. If your command exists on the item, then you’re all set. However if you wish to route this binding to a command on your ContentPage‘s view model as in this example you can use the newly supported 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"/>