Xamarin Forms CollectionView 点击手势或选择更改不起作用

Xamarin Forms CollectionView Tap Gesture or Selection Changed not working

Xamarin.Forms 我的问题至少出现在 android。

我有一个使用分组的集合视图。我想让每个 collectionview 组都可以点击。我已经尝试在第二个数据模板中向 StackLayout 添加一个 tapGesture 识别器,在顶部数据模板中添加标签和 CollectionView 本身,我也尝试将它们作为点击和命令。最后,我在 collectionView 上尝试了 SelectionChanged。

单击 collectionview 上的任意位置不会在 ViewModel 或 View.cs

中命中任何断点

我希望这样,点击某个项目会向 ViewModel 提供某种参数,以便它知道 CollectionView 中的哪个项目被点击了

这是我的XAML,如果有人能看出我犯了什么错误那就太棒了!谢谢

<CollectionView Grid.Row="1"  ItemsSource="{Binding ResponseCollection}" SelectionMode="Single" SelectionChanged="ResponseCollectionView_SelectionChanged" x:Name="ResponseCollectionView" IsGrouped="True" >

                <CollectionView.GroupHeaderTemplate>
                    <DataTemplate >
                        <Label BackgroundColor="White" Padding="10,10,10,0" Text="{Binding Response}">
                        </Label>

                    </DataTemplate>
                </CollectionView.GroupHeaderTemplate>

                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <ScrollView>
                            <Grid Padding="10" BackgroundColor="White">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="1"/>
                                </Grid.RowDefinitions>

                                <StackLayout Grid.Row="0" Orientation="Horizontal">

                                    <Label TextColor="Red" Text="{Binding Statistic}"/>
                                    <Label TextColor="Red" Text="{Binding Amount}"/>
                                </StackLayout>
                                
                                <BoxView Grid.Row="1" BackgroundColor="Gray" HorizontalOptions="FillAndExpand"></BoxView>
                            </Grid>
                        </ScrollView>
                    </DataTemplate>
                </CollectionView.ItemTemplate>

            </CollectionView>

使用relative binding

  1. 为容器(或 ContentPage)添加名称
  2. 为您的点击命令绑定使用相对绑定
<StackLayout x:Name="PageView">
    <CollectionView>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding Path=BindingContext.ViewModelCommand, Source={x:Reference PageView}}" />
                    </StackLayout.GestureRecognizers>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>

已更新以包含参数:

<TapGestureRecognizer
    Command="{Binding Path=BindingContext.ViewModelCommand, Source={x:Reference PageView}}"
    CommandParameter="{Binding .}"/>