0

我正在尝试在 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");
    }
4

1 回答 1

1

问题不在于 TouchEffect 问题在于您的 Binding,因为在DataTemplateif BindingContextnot specified 内部会被覆盖(更改)为值 ofItemsSource而不是继承的BindingContext.

您绑定了错误的BindingContext(源),在调试过程中,如果您打开并查看“xaml 绑定失败”窗口/窗格,您将看到明确的明确错误,如LongPressCommandSelection无法在中找到FolderFiles,您应该在此指定您的绑定源案子:

<ContentPage x:Name="thisPage" ...>
...
<Grid xct:TouchEffect.LongPressCommand="{Binding Source={x:Reference thisPage}, Path=BindingContext.LongPressCommandSelection}"
于 2021-05-03T02:53:55.447 回答