diff --git a/src/Uno.UI/Extensions/EnumerableExtensions.cs b/src/Uno.UI/Extensions/EnumerableExtensions.cs index 0373f5eb3695..6a1c11006129 100644 --- a/src/Uno.UI/Extensions/EnumerableExtensions.cs +++ b/src/Uno.UI/Extensions/EnumerableExtensions.cs @@ -34,5 +34,43 @@ public static List SelectToList(this UIElementCollection sourc return output; } + + /// + /// ToDictionary that doesn't throw on duplicated key. The first value is kept per key. + /// + public static Dictionary ToDictionaryKeepFirst( + this IEnumerable source, + Func keySelector, + Func elementSelector + ) where TKey : notnull + { + var result = new Dictionary(); + + foreach (var item in source) + { + result.TryAdd(keySelector(item), elementSelector(item)); + } + + return result; + } + + /// + /// ToDictionary that doesn't throw on duplicated key. The last value is kept per key. + /// + public static Dictionary ToDictionaryKeepLast( + this IEnumerable source, + Func keySelector, + Func elementSelector + ) where TKey : notnull + { + var result = new Dictionary(); + + foreach (var item in source) + { + result[keySelector(item)] = elementSelector(item); + } + + return result; + } } } diff --git a/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.iOS.cs b/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.iOS.cs index 6cb52a4a89b5..376b7f004bc4 100644 --- a/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.iOS.cs +++ b/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.iOS.cs @@ -557,13 +557,13 @@ private CGSize PrepareLayoutInternal(bool createLayoutInfo, bool isCollectionCha // We are layouting after an INotifyCollectionChanged operation(s). Cache the previous element sizes, under their new index // paths, so we can reuse them in order not to have to lay out elements with different databound sizes with their static size. oldItemSizes = _itemLayoutInfos.SelectMany(kvp => kvp.Value) - .ToDictionary( + .ToDictionaryKeepLast( kvp => OffsetIndexForPendingChanges(kvp.Key, NativeListViewBase.ListViewItemElementKind), kvp => (CGSize?)kvp.Value.Size ); oldGroupHeaderSizes = _supplementaryLayoutInfos .UnoGetValueOrDefault(NativeListViewBase.ListViewSectionHeaderElementKind)? - .ToDictionary( + .ToDictionaryKeepLast( kvp => OffsetIndexForPendingChanges(kvp.Key, NativeListViewBase.ListViewSectionHeaderElementKind).Section, kvp => (CGSize?)kvp.Value.Size );