我正在 .Net MAUI 中开发一个使用拖放功能的 UI。拖放不是问题;工作正常。问题是我想在拖动图像时更改图像显示。不是拖动源也不是放置目标,而是光标后面的图像。
下面的示例显示了该行为。我想改变它,以便随着光标移动的图像是不同的。
如果我对圣诞节有两个愿望,那就是隐藏 MAUI 在拖动图像时放在图像顶部的“复制”工具提示图形。
XAML
<Frame Margin="20" HorizontalOptions="Center" BorderColor="LightBlue" ZIndex="1" MaximumWidthRequest="400" MaximumHeightRequest="400" BackgroundColor="LightBlue">
<Image x:Name="reds" Source="reds_trans_bg.png" MaximumHeightRequest="100" MinimumHeightRequest="100" MaximumWidthRequest="100" MinimumWidthRequest="100">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" DragStarting="DragGestureRecognizer_DragStarting_2" />
</Image.GestureRecognizers>
</Image>
</Frame>
<Frame Margin="20" HorizontalOptions="Center" BorderColor="LightBlue" ZIndex="1" MaximumWidthRequest="400" MaximumHeightRequest="400" BackgroundColor="LightBlue">
<Frame.GestureRecognizers>
<DropGestureRecognizer AllowDrop="True" Drop="DropGestureRecognizer_Drop_2" />
</Frame.GestureRecognizers>
<VerticalStackLayout VerticalOptions="Center" BackgroundColor="LightBlue">
<Image Source="washer_trans_bg.png" BackgroundColor="LightBlue" ZIndex="100" MaximumWidthRequest="100" MaximumHeightRequest="100" >
</Image>
<HorizontalStackLayout HorizontalOptions="Center" BackgroundColor="LightBlue">
<Label x:Name="contents" Text="test" BackgroundColor="LightBlue" TextColor="White" FontAttributes="Bold">
</Label>
</HorizontalStackLayout>
</VerticalStackLayout>
</Frame>
代码
private void DragGestureRecognizer_DragStarting_2(object sender, DragStartingEventArgs e)
{
var image = (sender as Element)?.Parent as Image;
e.Data.Properties.Add("Text", "Red");
}
private void DropGestureRecognizer_Drop_2(object sender, DropEventArgs e)
{
var data = e.Data.Properties["Text"].ToString();
contents.Text = $"Red: {++count}";
}
据我所知,唯一可以开箱即用地执行此操作的平台是 iOS 和 Mac Catalyst。
来自文档。
void OnDragStarting(object sender, DragStartingEventArgs e)
{
#if IOS || MACCATALYST
Func<UIKit.UIDragPreview> action = () =>
{
var image = UIKit.UIImage.FromFile("dotnet_bot.png");
UIKit.UIImageView imageView = new UIKit.UIImageView(image);
imageView.ContentMode = UIKit.UIViewContentMode.Center;
imageView.Frame = new CoreGraphics.CGRect(0, 0, 250, 250);
return new UIKit.UIDragPreview(imageView);
};
e.PlatformArgs.SetPreviewProvider(action);
#endif
}