|
1 | 1 | #nullable disable |
2 | 2 | using System; |
3 | 3 | using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
4 | 5 | using System.Text; |
5 | 6 | using Foundation; |
6 | 7 | using Microsoft.Maui.Handlers; |
@@ -63,10 +64,19 @@ public CollectionViewHandler2(PropertyMapper mapper = null) : base(mapper ?? Map |
63 | 64 |
|
64 | 65 | public partial class CollectionViewHandler2 : ItemsViewHandler2<ReorderableItemsView> |
65 | 66 | { |
| 67 | + IItemsLayout _currentItemsLayout; |
| 68 | + PropertyChangedEventHandler _itemsLayoutPropertyChangedHandler; |
| 69 | + |
66 | 70 | // Reorderable |
67 | 71 | protected override ItemsViewController2<ReorderableItemsView> CreateController(ReorderableItemsView itemsView, UICollectionViewLayout layout) |
68 | 72 | => new ReorderableItemsViewController2<ReorderableItemsView>(itemsView, layout); |
69 | 73 |
|
| 74 | + protected override void DisconnectHandler(UIView platformView) |
| 75 | + { |
| 76 | + UnsubscribeFromItemsLayoutPropertyChanged(); |
| 77 | + base.DisconnectHandler(platformView); |
| 78 | + } |
| 79 | + |
70 | 80 | public static void MapCanReorderItems(CollectionViewHandler2 handler, ReorderableItemsView itemsView) |
71 | 81 | { |
72 | 82 | (handler.Controller as ReorderableItemsViewController2<ReorderableItemsView>)?.UpdateCanReorderItems(); |
@@ -202,21 +212,41 @@ public static void MapItemSizingStrategy(CollectionViewHandler2 handler, Structu |
202 | 212 |
|
203 | 213 | void SubscribeToItemsLayoutPropertyChanged(IItemsLayout itemsLayout) |
204 | 214 | { |
| 215 | + // Unsubscribe from the previous ItemsLayout if it exists |
| 216 | + UnsubscribeFromItemsLayoutPropertyChanged(); |
| 217 | + |
205 | 218 | if (itemsLayout is not null) |
206 | 219 | { |
207 | | - itemsLayout.PropertyChanged += (sender, args) => |
| 220 | + // Create the handler if it doesn't exist |
| 221 | + if (_itemsLayoutPropertyChangedHandler == null) |
208 | 222 | { |
209 | | - if (args.PropertyName == nameof(ItemsLayout.SnapPointsAlignment) || |
210 | | - args.PropertyName == nameof(ItemsLayout.SnapPointsType) || |
211 | | - args.PropertyName == nameof(GridItemsLayout.VerticalItemSpacing) || |
212 | | - args.PropertyName == nameof(GridItemsLayout.HorizontalItemSpacing) || |
213 | | - args.PropertyName == nameof(GridItemsLayout.Span) || |
214 | | - args.PropertyName == nameof(LinearItemsLayout.ItemSpacing)) |
215 | | - |
| 223 | + _itemsLayoutPropertyChangedHandler = (sender, args) => |
216 | 224 | { |
217 | | - UpdateLayout(); |
218 | | - } |
219 | | - }; |
| 225 | + if (args.PropertyName == nameof(ItemsLayout.SnapPointsAlignment) || |
| 226 | + args.PropertyName == nameof(ItemsLayout.SnapPointsType) || |
| 227 | + args.PropertyName == nameof(GridItemsLayout.VerticalItemSpacing) || |
| 228 | + args.PropertyName == nameof(GridItemsLayout.HorizontalItemSpacing) || |
| 229 | + args.PropertyName == nameof(GridItemsLayout.Span) || |
| 230 | + args.PropertyName == nameof(LinearItemsLayout.ItemSpacing)) |
| 231 | + |
| 232 | + { |
| 233 | + UpdateLayout(); |
| 234 | + } |
| 235 | + }; |
| 236 | + } |
| 237 | + |
| 238 | + // Subscribe to the new ItemsLayout |
| 239 | + itemsLayout.PropertyChanged += _itemsLayoutPropertyChangedHandler; |
| 240 | + _currentItemsLayout = itemsLayout; |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + void UnsubscribeFromItemsLayoutPropertyChanged() |
| 245 | + { |
| 246 | + if (_currentItemsLayout is not null && _itemsLayoutPropertyChangedHandler is not null) |
| 247 | + { |
| 248 | + _currentItemsLayout.PropertyChanged -= _itemsLayoutPropertyChangedHandler; |
| 249 | + _currentItemsLayout = null; |
220 | 250 | } |
221 | 251 | } |
222 | 252 | } |
|
0 commit comments