Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Platform/iOS/DragAndDropDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ session.LocalDragSession.Items[0].LocalObject is CustomLocalStateData cdi &&
_viewHandler.VirtualView is View view)
{
HandleDrop(view, cdi.DataPackage, session, new PlatformDropEventArgs(cdi.View.Handler.PlatformView as UIView, interaction, session));
HandleDropCompleted(cdi.View, new PlatformDropCompletedEventArgs(cdi.View.Handler.PlatformView as UIView, interaction, session));
HandleDropCompleted(cdi.View, new PlatformDropCompletedEventArgs(cdi.View.Handler?.PlatformView as UIView, interaction, session));
Copy link
Copy Markdown
Contributor Author

@MartyIX MartyIX Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the bug1 for me.

Alternatively, one can extract cdi.View.Handler.PlatformView to a variable. But I'm not exactly sure what the expected behavior is.

Please tell me.

Footnotes

  1. If it is supported scenario.

}
else if (_viewHandler.VirtualView is View v)
{
Expand Down
19 changes: 19 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue28416.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue28416">
<VerticalStackLayout x:Name="stackLayout" AutomationId="StackLayout">
<Rectangle x:Name="dragObject" AutomationId="DragObject" Stroke="Red" Fill="DarkBlue" StrokeThickness="4" HeightRequest="200" WidthRequest="200">
<Rectangle.GestureRecognizers>
<DragGestureRecognizer DropCompleted="DragGestureRecognizer_DropCompleted" />
</Rectangle.GestureRecognizers>
</Rectangle>

<Image x:Name="dragTarget" AutomationId="DragTarget" BackgroundColor="Silver" HeightRequest="300" WidthRequest="250">
<Image.GestureRecognizers>
<DropGestureRecognizer Drop="DropGestureRecognizer_Drop" />
</Image.GestureRecognizers>
</Image>
<Label AutomationId="InstructionsLabel" Text="Drag the blue square onto the silver element" />
</VerticalStackLayout>
</ContentPage>
25 changes: 25 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue28416.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 28416, "Crash in drag-n-drop with dragged element destroyed before drop is completed", PlatformAffected.iOS)]
public partial class Issue28416 : ContentPage
{
public Issue28416()
{
InitializeComponent();
}

private void DropGestureRecognizer_Drop(object sender, DropEventArgs e)
{
stackLayout.Remove(dragObject);

// This causes the crash.
dragObject.Handler = null;

stackLayout.Add(new Label { AutomationId = "DropResult", Text = "Dropped!" });
}

private void DragGestureRecognizer_DropCompleted(object sender, DropCompletedEventArgs e)
{
stackLayout.Add(new Label { AutomationId = "DropCompletedResult", Text = "Completed!" });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if TEST_FAILS_ON_WINDOWS && TEST_FAILS_ON_ANDROID // "DropCompletedResult" does not appear on Windows. Issues: https://github.com/dotnet/maui/issues/28448 and https://github.com/dotnet/maui/issues/17554
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

[Category(UITestCategories.DragAndDrop)]
public class Issue28416 : _IssuesUITest
{
public Issue28416(TestDevice testDevice) : base(testDevice)
{
}

protected override bool ResetAfterEachTest => true;

public override string Issue => "Crash in drag-n-drop with dragged element destroyed before drop is completed";

[Test]
public void DragAndDropWithAnElementThatIsRemoved()
{
App.WaitForElement("InstructionsLabel");
App.DragAndDrop("DragObject", "DragTarget");
App.WaitForElement("DropResult");
App.WaitForElement("DropCompletedResult");
}
}
#endif
Loading