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