fixing virtual event issue

This commit is contained in:
Shannon
2021-01-12 15:38:18 +11:00
parent 4a3525ece3
commit f388c7406d
2 changed files with 24 additions and 8 deletions

View File

@@ -8,7 +8,7 @@ namespace Umbraco.Core.Collections
/// Allows clearing all event handlers
/// </summary>
/// <typeparam name="TValue"></typeparam>
public class EventClearingObservableCollection<TValue> : ObservableCollection<TValue>
public class EventClearingObservableCollection<TValue> : ObservableCollection<TValue>, INotifyCollectionChanged
{
public EventClearingObservableCollection()
{
@@ -22,12 +22,20 @@ namespace Umbraco.Core.Collections
{
}
// need to override to clear
public override event NotifyCollectionChangedEventHandler CollectionChanged;
// need to explicitly implement with event accessor syntax in order to override in order to to clear
// c# events are weird, they do not behave the same way as other c# things that are 'virtual',
// a good article is here: https://medium.com/@unicorn_dev/virtual-events-in-c-something-went-wrong-c6f6f5fbe252
// and https://stackoverflow.com/questions/2268065/c-sharp-language-design-explicit-interface-implementation-of-an-event
private NotifyCollectionChangedEventHandler _changed;
event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged
{
add { _changed += value; }
remove { _changed -= value; }
}
/// <summary>
/// Clears all event handlers for the <see cref="CollectionChanged"/> event
/// </summary>
public void ClearCollectionChangedEvents() => CollectionChanged = null;
public void ClearCollectionChangedEvents() => _changed = null;
}
}

View File

@@ -18,7 +18,7 @@ namespace Umbraco.Core.Collections
/// </remarks>
/// <typeparam name="TValue">The type of elements contained in the BindableCollection</typeparam>
/// <typeparam name="TKey">The type of the indexing key</typeparam>
public class ObservableDictionary<TKey, TValue> : ObservableCollection<TValue>, IReadOnlyDictionary<TKey, TValue>, IDictionary<TKey, TValue>
public class ObservableDictionary<TKey, TValue> : ObservableCollection<TValue>, IReadOnlyDictionary<TKey, TValue>, IDictionary<TKey, TValue>, INotifyCollectionChanged
{
protected Dictionary<TKey, int> Indecies { get; }
protected Func<TValue, TKey> KeySelector { get; }
@@ -77,13 +77,21 @@ namespace Umbraco.Core.Collections
#endregion
// need to override to clear
public override event NotifyCollectionChangedEventHandler CollectionChanged;
// need to explicitly implement with event accessor syntax in order to override in order to to clear
// c# events are weird, they do not behave the same way as other c# things that are 'virtual',
// a good article is here: https://medium.com/@unicorn_dev/virtual-events-in-c-something-went-wrong-c6f6f5fbe252
// and https://stackoverflow.com/questions/2268065/c-sharp-language-design-explicit-interface-implementation-of-an-event
private NotifyCollectionChangedEventHandler _changed;
event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged
{
add { _changed += value; }
remove { _changed -= value; }
}
/// <summary>
/// Clears all <see cref="CollectionChanged"/> event handlers
/// </summary>
public void ClearCollectionChangedEvents() => CollectionChanged = null;
public void ClearCollectionChangedEvents() => _changed = null;
public bool ContainsKey(TKey key)
{