我正在尝试在 Maui 项目中的两个 CollectionView 之间实现拖放。
在我的示例中,我在第一个 CollectionView 中有一个歌曲集合,在第二个集合中有一个播放列表组。歌曲 CollectionView 看起来像这样:
<CollectionView
Grid.Row="2"
Margin="0,4"
EmptyView="You have no songs in your library!"
ItemsSource="{Binding Songs}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="m:Song">
<SwipeView android:SwipeView.SwipeTransitionMode="Reveal">
<SwipeView.RightItems>
<SwipeItems SwipeBehaviorOnInvoked="Close">
<SwipeItem
BackgroundColor="Pink"
Command="{Binding Source={RelativeSource AncestorType={x:Type vm:PlayListViewModel}}, Path=FavoriteCommand}"
CommandParameter="{Binding .}"
Text="Favorite" />
</SwipeItems>
</SwipeView.RightItems>
<Frame Margin="4" Padding="4">
<cell:SongArtist />
<Frame.GestureRecognizers>
<DragGestureRecognizer
CanDrag="True"
DragStartingCommand="{Binding Source={RelativeSource AncestorType={x:Type vm:PlayListViewModel}}, Path=DragStartedCommand}"
DragStartingCommandParameter="{Binding .}" />
</Frame.GestureRecognizers>
</Frame>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
播放列表集合视图如下所示:
<CollectionView
Grid.Row="5"
Margin="0,4"
EmptyView="You have no songs in your playlist!"
ItemsSource="{Binding PlayList}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="m:Song">
<SwipeView android:SwipeView.SwipeTransitionMode="Reveal">
<SwipeView.RightItems>
<SwipeItems SwipeBehaviorOnInvoked="Close">
<SwipeItem
BackgroundColor="Red"
Command="{Binding Source={RelativeSource AncestorType={x:Type vm:PlayListViewModel}}, Path=DeleteCommand}"
CommandParameter="{Binding .}"
Text="Delete" />
</SwipeItems>
</SwipeView.RightItems>
<Frame Margin="4" Padding="4">
<cell:SongArtist />
<Frame.GestureRecognizers>
<DropGestureRecognizer AllowDrop="True" DropCommand="{Binding Source={RelativeSource AncestorType={x:Type vm:PlayListViewModel}}, Path=SongDropped}" />
</Frame.GestureRecognizers>
</Frame>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
DragStartedCommand 确实会触发。 SongDroppedCommand 则不然。这是我的 ViewModel 中的代码:
[RelayCommand]
void DragStarted(Song song)
{
dragged_song = song;
Songs.Remove(song);
Debug.WriteLine($"DragStarted {song.Name}");
}
[RelayCommand]
void SongDropped(Song song)
{
PlayList.Add(song);
Debug.WriteLine($"SongDropped");
}
不确定为什么 SongDroppedCommand 没有触发。
提前致谢!
查看评论。在@Jason 的帮助下,这就是答案。