internal void OnItemsSourceChanging(IEnumerable oldValue, IEnumerable newValue) { // wrap up enumerable, IList, IList<T>, and IReadOnlyList<T> var itemSourceAsList = newValue?.ToReadOnlyList(); // allow interception of itemSource var onCollectionChanged = _onCollectionChanged; itemSourceAsList = OnItemsSourceChanging(itemSourceAsList, ref onCollectionChanged); if (itemSourceAsList == null) { throw new InvalidOperationException( "OnItemsSourceChanging must return non-null itemSource as IReadOnlyList"); } // keep callback alive _onCollectionChangedProxy = onCollectionChanged; // dispatch CollectionChangedEvent to ItemView without a strong reference to ItemView and // synchronize dispatch and element access via CollectionSynchronizationContext protocol _itemSourceProxy?.Dispose(); _itemSourceProxy = new ItemsSourceProxy( itemsSource: newValue, itemsSourceAsList: itemSourceAsList, onCollectionChanged: new WeakReference <NotifyCollectionChangedEventHandler>(_onCollectionChangedProxy) ); OnItemsSourceChanged(oldValue, newValue); }
private void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add) { foreach (ListElement item in e.NewItems) { var add = true; foreach (var i in ItemsSourceProxy) { if (!string.IsNullOrEmpty(i.Id) && i.Id == item.Id) { i.ActionType = item.ActionType; i.Description = item.Description; i.DescriptionFontSize = item.DescriptionFontSize; i.DescriptionTextColor = item.DescriptionTextColor; i.FontSize = item.FontSize; i.Image = item.Image; i.Resource = item.Resource; i.TextColor = item.TextColor; i.Title = item.Title; add = false; break; } } if (add) { ItemsSourceProxy.Add(item); } } } }
public void Draw( ) { foreach (ListElement item in ItemsSource) { var add = true; foreach (var i in ItemsSourceProxy) { if (!string.IsNullOrEmpty(i.Id) && i.Id == item.Id) { i.ActionType = item.ActionType; i.Description = item.Description; i.DescriptionFontSize = item.DescriptionFontSize; i.DescriptionTextColor = item.DescriptionTextColor; i.FontSize = item.FontSize; i.Image = item.Image; i.Resource = item.Resource; i.TextColor = item.TextColor; i.Title = item.Title; add = false; break; } } if (add) { ItemsSourceProxy.Add(item); } } }
public ItemView() { _onCollectionChanged = _onCollectionChangedProxy = OnCollectionChanged; _itemSourceProxy = new ItemsSourceProxy( itemsSource: Enumerable.Empty <object>(), itemsSourceAsList: OnInitializeItemSource(), onCollectionChanged: new WeakReference <NotifyCollectionChangedEventHandler>(_onCollectionChangedProxy) ); }