在 .NET MAUI 中拖动错误的目标(图像)后,AllowDrop 不起作用

问题描述 投票:0回答:1

在拖动对象并更改AllowDrop 属性时,我在.met maui 中遇到了一个奇怪的错误/错误。 因此,我有三张图像,其中一张被拖到另外两张上并激活验证逻辑。 这是拖动图像:

 <Image Grid.Row="0"
        x:Name="image1"
        Grid.Column="0"
        Source="{Binding FirstImageRoute}"
        Opacity="{Binding SecondImageOpacity}">
     <Image.GestureRecognizers>
         <DragGestureRecognizer
                     CanDrag="True"
                     DropCompletedCommand="{Binding DragCompleteCommand}"
                     DragStartingCommand="{Binding DragStartingCommand}"
         DragStartingCommandParameter="{x:Reference image1}">
         </DragGestureRecognizer>
     </Image.GestureRecognizers>
 </Image>

如您所见,我使用 Observable 属性和 DragGestureRecognizer。我有一个名为 isAllow 的 ObservableProperty 来控制是否允许放置。

以下是两个目标图像的 XAML 定义,其中第一个图像将被删除:

<Image x:Name="image2"
    Grid.Row="1"
       Grid.Column="1"
       Source="{Binding SecondImageRoute}"
       Opacity="{Binding FirstImageOpacity, Mode=TwoWay}">
    <Image.GestureRecognizers>
        <DropGestureRecognizer AllowDrop="{Binding IsAllow}"
               DragOverCommand="{Binding DragOverCommand}"
               DropCommand="{Binding DropCommand}"
               x:Name="drop1">
            <DropGestureRecognizer.DragOverCommandParameter>
                <Binding Source="{x:Reference image2}"/>
            </DropGestureRecognizer.DragOverCommandParameter>
            <DropGestureRecognizer.DropCommandParameter>
                <Binding Source="{x:Reference image2}"/>
            </DropGestureRecognizer.DropCommandParameter>
        </DropGestureRecognizer>
    </Image.GestureRecognizers>
</Image>

<Image x:Name="image3"
       Grid.Row="1"
       Grid.Column="0"
       Source="{Binding FirstImageRoute}"
       Opacity="{Binding SecondImageOpacity, Mode=TwoWay}">
    <Image.GestureRecognizers>
        <DropGestureRecognizer AllowDrop="{Binding IsAllow}"
               DragOverCommand="{Binding DragOverCommand}"
               DropCommand="{Binding DropCommand}"
               x:Name="drop2">
            <DropGestureRecognizer.DragOverCommandParameter>
                <Binding Source="{x:Reference image3}"/>
            </DropGestureRecognizer.DragOverCommandParameter>
            <DropGestureRecognizer.DropCommandParameter>
                <Binding Source="{x:Reference image3}"/>
            </DropGestureRecognizer.DropCommandParameter>
        </DropGestureRecognizer>
    </Image.GestureRecognizers>
</Image>

在我的 ViewModel 中,我编写了命令来处理拖放逻辑:

[ObservableProperty]
public double firstImageOpacity = 0.5;

[ObservableProperty]
public double secondImageOpacity = 0.5;

public bool isDroppedSuccessfully;

[ObservableProperty]
public bool isAllow = true;

及方法:

[RelayCommand]
 private void DragCompleted()
 {
     if (!isDroppedSuccessfully)
     {
         IsAllow = true;
     }
     isDroppedSuccessfully = false;
 }

 [RelayCommand]
 public void DragStarting(Image image)
 {
     if (image != null)
     {
         var filename = image.Source.ToString();
         TempImage = filename.Replace("File: ", "");
     }
 }



 [RelayCommand]
 public void DragOver(Image image)
 {
     var secondImage = image.Source.ToString().Replace("File: ", "");

     IsAllow = true;

     if (TempImage == secondImage)
     {
         IsAllow = true;
         return;
     }
     else
     {
         if (IsAllow)
         {
             IsAllow = false;
         }
     }
 }

 [RelayCommand]
 public void Drop(Image image)
 {
     IsAllow = true;

     isDroppedSuccessfully = true;

     image.Opacity = 1;
 }

 [RelayCommand]
 public void DragComplete()
 {
     if (!isDroppedSuccessfully)
     {
         IsAllow = true;
     }
     isDroppedSuccessfully = false;
 }

基本逻辑 - 我们将图像拖到两张卡片上,当我们将图像悬停在卡片(Image)上时,我们检查卡片的图像是否与拖动的图像匹配。如果图像匹配 - 执行一些逻辑(设置不透明度) 我做了一个使用 [ObservableProperty] isAllow 进行验证的小测试逻辑。

因此,当我将图像拖到错误的图像上时,就会出现问题,在这种情况下,我什至无法再将其放入正确的图像中。

换句话说,如果用户立即将图像拖动到正确的卡(图像)中,逻辑工作正常,但如果我拖动并悬停在错误的卡(图像)上,我就无法再将图像放入正确的卡中。 (如下图所示) 有人有什么想法吗?也许这根本不是一个错误,而是我的验证逻辑有问题并且我没有正确实现验证?

测试程序运行的

Gifhttps://imgur.com/a/80v4lZf

c# .net maui community-toolkit-mvvm
1个回答
0
投票

您可以将用于匹配的代码放在 drop 命令中,而不是放在 dropover 命令中。这是在放下图像之前将

IsAllow
设置为 true 的方法。

[RelayCommand]
 public void Drop(Image image)
 {
     IsAllow = true;

     var secondImage = image.Source.ToString().Replace("File: ", "");

     if (TempImage == secondImage)
     {
         IsAllow = true;
         isDroppedSuccessfully = true;
         image.Opacity = 1;
         return;
     }
     else
     {
         if (IsAllow)
         {
             IsAllow = false;
         }
     }

     
 }
© www.soinside.com 2019 - 2024. All rights reserved.