From 3c6e6e4025836622d4a1fdfb96e10f8c8de39c69 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Tue, 2 Nov 2021 11:08:29 -0400 Subject: [PATCH] fix(listview): [WASM] Support removing item while dragging Fixes error that could occur when removing an item from collection while it was being dragged. --- .../ItemsStackPanel/ItemsStackPanelLayout.managed.cs | 4 ++-- .../ListViewBase/VirtualizingPanelLayout.managed.cs | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Uno.UI/UI/Xaml/Controls/ItemsStackPanel/ItemsStackPanelLayout.managed.cs b/src/Uno.UI/UI/Xaml/Controls/ItemsStackPanel/ItemsStackPanelLayout.managed.cs index 6965f4d68a52..ab3a16577f37 100644 --- a/src/Uno.UI/UI/Xaml/Controls/ItemsStackPanel/ItemsStackPanelLayout.managed.cs +++ b/src/Uno.UI/UI/Xaml/Controls/ItemsStackPanel/ItemsStackPanelLayout.managed.cs @@ -21,9 +21,9 @@ partial class ItemsStackPanelLayout protected override Line CreateLine(GeneratorDirection fillDirection, double extentOffset, double availableBreadth, Uno.UI.IndexPath nextVisibleItem) { - if (ShouldInsertReorderingView(extentOffset)) + if (ShouldInsertReorderingView(extentOffset) && GetAndUpdateReorderingIndex() is { } reorderingIndex) { - nextVisibleItem = GetAndUpdateReorderingIndex().Value; + nextVisibleItem = reorderingIndex; } var item = GetFlatItemIndex(nextVisibleItem); diff --git a/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.managed.cs b/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.managed.cs index 3aecf4513bac..4b5a7173ff58 100644 --- a/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.managed.cs +++ b/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.managed.cs @@ -1094,8 +1094,12 @@ protected bool ShouldInsertReorderingView(double extentOffset) { if (reorder.index is null) { - reorder.index = XamlParent!.GetIndexPathFromItem(reorder.item); - _pendingReorder = reorder; // _pendingReorder is a struct! + var itemIndex = XamlParent!.GetIndexPathFromItem(reorder.item); + if (itemIndex.Row >= 0) // GetIndexPathFromItem() will return Row=-1 if item is not found, which may happen eg if it's been removed from the collection during dragging. Prefer to leave index null in this case. + { + reorder.index = itemIndex; + _pendingReorder = reorder; // _pendingReorder is a struct! + } } return reorder.index;