嵌套网格 TapGestureRecognizer 不工作?
Posted
技术标签:
【中文标题】嵌套网格 TapGestureRecognizer 不工作?【英文标题】:Nested Grid TapGestureRecognizer not working? 【发布时间】:2021-06-29 11:52:18 【问题描述】:我是 Xamarin 的新手。我一直在尝试点击网格的每一行,但不幸的是没有工作。我有一个带有 A 网格的 CollectionView
,在 row=1 上的这个网格内我有另一个带有新网格的 CollectionView
。
我的问题是当我点击行 onlu row=0 并且 row=2 正在触发时,而不是 row=1 我有另一个 COllectionView
。我在下面附上了我的代码。
<RefreshView Command="Binding LoadAlertsCommand" IsRefreshing="Binding IsBusy, Mode=TwoWay" IsVisible="Binding RefreshViewVisible">
<CollectionView x:Name="AlertsListView"
ItemsSource="Binding Alerts"
SelectionMode="None" >
<CollectionView.ItemTemplate>
<DataTemplate>
<SwipeView x:DataType="model:AlertsTable" Threshold="250">
<SwipeView.RightItems>
<SwipeItems Mode="Execute">
<SwipeItem Text="Delete"
BackgroundColor="#ff453a"
Command="Binding Source=RelativeSource AncestorType=x:Type local:AlertViewModel, Path=DeleteCommand"
CommandParameter="Binding ."/>
</SwipeItems>
</SwipeView.RightItems>
<Grid Padding="15,5,15,0" x:DataType="model:AlertsTable" x:Name="Grid1" BackgroundColor="Black">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<BoxView Color="#292929" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="4"/>
<Label x:Name="TextLabel" Grid.Row="0" Grid.Column="0" Text="Binding title" TextColor="White" FontSize="20" FontAttributes="Bold"/>
<Label x:Name="RightTextLabel" Grid.Row="0" Grid.Column="1" FontSize="15" Text="Binding date" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" TextColor="White"/>
<Label Text="x:Static fontAwesome:FontAwesomeIcons.Circle"
IsVisible="Binding wasRead"
Grid.Row="0" Grid.Column="3"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
FontSize="Medium"
TextColor="White"
FontFamily="fa5r"/>
<!--<Frame >-->
<CollectionView x:Name="ConditionsListView"
ItemsSource="Binding alertmetadata.Conditions"
SelectionMode="None" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="0" HeightRequest="120">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid x:DataType="model:AlertConditionEx">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Text="Binding LeftItem.NS" Grid.Row="0" Grid.Column="0" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" TextColor="White"/>
<Image Source="x:Binding OS , Converter=StaticResource ImageConverter" Grid.Row="0" Grid.Column="1" HorizontalOptions="Center" VerticalOptions="CenterAndExpand"
HeightRequest="40"></Image>
<Label Text="Binding RightItem.NS" Grid.Row="0" Grid.Column="2" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" TextColor="White"/>
<Image Source="x:Binding OS , Converter=StaticResource ChartImageConverter" Grid.Row="0" Grid.Column="3"
HeightRequest="40"></Image>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Label Text="..." TextColor="White" FontAttributes="Bold" FontSize="Large" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" IsVisible="Binding alertmetadata.ThreeDots" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand"/>
<BoxView VerticalOptions="FillAndExpand" HeightRequest="1" Color="#59595b"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="4"/>
<!--</Frame>-->
<Grid.GestureRecognizers>
<TapGestureRecognizer
NumberOfTapsRequired="1"
Command="Binding Source=RelativeSource AncestorType=x:Type local:AlertViewModel, Path=AlertsTapped"
CommandParameter="Binding .">
</TapGestureRecognizer>
</Grid.GestureRecognizers>
</Grid>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
【问题讨论】:
你能在你的帖子中添加你不工作的代码吗? 【参考方案1】:Tap 手势不起作用,因为CollectionView
与BoxView
或Label
不同,CollectionView
本身能够接收动作(点击项目),因此会与外部手势发生冲突。
为了解决这个问题,我们可以在内部collectionview上设置额外的点击手势。
<CollectionView x:Name="ConditionsListView"
ItemsSource="Binding alertmetadata.Conditions"
SelectionMode="None" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="0" HeightRequest="120">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid x:DataType="model:AlertConditionEx">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Text="Binding LeftItem.NS" Grid.Row="0" Grid.Column="0" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" TextColor="White"/>
<Image Source="x:Binding OS , Converter=StaticResource ImageConverter" Grid.Row="0" Grid.Column="1" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" HeightRequest="40"></Image>
<Label Text="Binding RightItem.NS" Grid.Row="0" Grid.Column="2" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" TextColor="White"/>
<Image Source="x:Binding OS , Converter=StaticResource ChartImageConverter" Grid.Row="0" Grid.Column="3" HeightRequest="40"></Image>
//add this code
<Grid.GestureRecognizers>
<TapGestureRecognizer
NumberOfTapsRequired="1"
Command="Binding Source=RelativeSource AncestorType=x:Type local:AlertViewModel, Path=AlertsTapped" CommandParameter="Binding .">
</TapGestureRecognizer>
</Grid.GestureRecognizers>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
参考
https://forums.xamarin.com/discussion/57539/listview-with-mvvm-tapgesturerecognizer-not-working.
【讨论】:
【参考方案2】:你必须像这样使用它:
Command="Binding Source=x:Reference AlertsListView,
Path=BindingContext.YourCommandName"
【讨论】:
它的工作方式和我的完全一样。我的问题是当我点击 Row = 0 和 Row =2 它的工作时,但是当我点击 Row=1 我的另一个收藏视图所在的位置时,什么都没有发生以上是关于嵌套网格 TapGestureRecognizer 不工作?的主要内容,如果未能解决你的问题,请参考以下文章