Xamarin 社区工具包 TouchEffect.Command 在 CollectionView 中不工作

Xamarin community toolkit TouchEffect.Command not working in CollectionView

我正在尝试在 CollectionView 中执行长按和短按命令。我正在使用 Xamarin.Community.ToolKit 但未调用目标 ViewModel 命令。 TouchEffect 在 CollectionView 之外时起作用。但当它位于 CollectionView 中时则不然。

下面是集合视图:

            <CollectionView x:Name="ItemsCollectionView"
                ItemsSource="{Binding FolderFiles}" 
                VerticalOptions="FillAndExpand"
                SelectionMode="{Binding SelectionModeFolderFile}"
                SelectedItems="{Binding SelectedFolderFiles}"
                >
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        
                        <Grid xct:TouchEffect.Command="{Binding ShortPressCommandSelection}"
                              xct:TouchEffect.LongPressCommand="{Binding LongPressCommandSelection}"
                              xct:TouchEffect.NativeAnimation="True"
                              >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="45"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="10*"></ColumnDefinition>
                                <ColumnDefinition Width="60*"></ColumnDefinition>
                                <ColumnDefinition Width="10*"></ColumnDefinition>
                                <ColumnDefinition Width="10*"></ColumnDefinition>
                                <ColumnDefinition Width="10*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>


                            <Image Grid.Row="0" Grid.Column="0"  x:Name="imgFileOrFolder" Source="{Binding ImgFileOrFolderSource}"   VerticalOptions="Center" WidthRequest="15" >
                            </Image>

                            <Label Grid.Row="0" VerticalTextAlignment="Center" Grid.Column="1" Text="{Binding Name}" 
                            LineBreakMode="NoWrap" 
                            Style="{DynamicResource ListItemTextStyle}" 
                            FontSize="12" />

                            <Image Grid.Row="0" Grid.Column="2"  x:Name="imgPlay" Source="{Binding ImgPlaySource}"  VerticalOptions="Center" WidthRequest="25">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer CommandParameter="{Binding Path}"
                                Tapped="PlayTapped" />
                                </Image.GestureRecognizers>
                            </Image>

                            <Image Grid.Row="0" Grid.Column="3"  x:Name="imgShuffle" Source="{Binding ImgShuffleSource}"  VerticalOptions="Center" WidthRequest="25">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer CommandParameter="{Binding Path}"
                                Tapped="ShuffleTapped" />
                                </Image.GestureRecognizers>
                            </Image>

                            <Image Grid.Row="0" Grid.Column="4"  x:Name="imgMoreInfo" Source="{Binding ImgMoreInfoSource}"  VerticalOptions="Center" WidthRequest="15">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer
                                Tapped="MoreInfoTapped" />
                                </Image.GestureRecognizers>
                            </Image>
                        </Grid>

                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

目标视图模型属性:

    public ICommand LongPressCommandSelection { get; set; }

    public ICommand ShortPressCommandSelection { get; set; }

构造函数:

        LongPressCommandSelection = new Command(LongPressCommand_SelectionChanged);
        ShortPressCommandSelection = new Command(ShortPressCommand_SelectionChanged);

方法:

    public void LongPressCommand_SelectionChanged()
    {
        Console.Write("selection changed");

    }

    public void ShortPressCommand_SelectionChanged()
    {
        Console.Write("selection changed");
    }

问题不在于 TouchEffect,问题在于您的绑定,因为在 DataTemplate 中,如果未指定,BindingContext 将被覆盖(更改)为 ItemsSource 的值继承的 BindingContext.

您正在绑定错误的 BindingContext(来源),在调试期间,如果您打开并查看您的“xaml 绑定失败”window/pane,您将看到明确的错误提示在 FolderFiles 中找不到类似 LongPressCommandSelection 的内容,在这种情况下,您应该指定绑定源:

<ContentPage x:Name="thisPage" ...>
...
<Grid xct:TouchEffect.LongPressCommand="{Binding Source={x:Reference thisPage}, Path=BindingContext.LongPressCommandSelection}"