Browse Source

Merge branch 'core2d-leak'

pull/464/head
Steven Kirk 11 years ago
parent
commit
a9e63ab6cd
  1. 26
      src/Perspex.Base/Collections/PerspexList.cs

26
src/Perspex.Base/Collections/PerspexList.cs

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using Perspex.Platform;
namespace Perspex.Collections namespace Perspex.Collections
{ {
@ -55,6 +56,7 @@ namespace Perspex.Collections
public class PerspexList<T> : IPerspexList<T>, IList public class PerspexList<T> : IPerspexList<T>, IList
{ {
private List<T> _inner; private List<T> _inner;
private NotifyCollectionChangedEventHandler _collectionChanged;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="PerspexList{T}"/> class. /// Initializes a new instance of the <see cref="PerspexList{T}"/> class.
@ -85,7 +87,11 @@ namespace Perspex.Collections
/// <summary> /// <summary>
/// Raised when a change is made to the collection's items. /// Raised when a change is made to the collection's items.
/// </summary> /// </summary>
public event NotifyCollectionChangedEventHandler CollectionChanged; public event NotifyCollectionChangedEventHandler CollectionChanged
{
add { _collectionChanged += value; }
remove { _collectionChanged -= value; }
}
/// <summary> /// <summary>
/// Raised when a property on the collection changes. /// Raised when a property on the collection changes.
@ -145,14 +151,14 @@ namespace Perspex.Collections
T old = _inner[index]; T old = _inner[index];
_inner[index] = value; _inner[index] = value;
if (CollectionChanged != null) if (_collectionChanged != null)
{ {
var e = new NotifyCollectionChangedEventArgs( var e = new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace, NotifyCollectionChangedAction.Replace,
value, value,
old, old,
index); index);
CollectionChanged(this, e); _collectionChanged(this, e);
} }
} }
} }
@ -168,6 +174,8 @@ namespace Perspex.Collections
set { this[index] = (T)value; } set { this[index] = (T)value; }
} }
public int SubscriberCount => _collectionChanged?.GetInvocationList().Length ?? 0;
/// <summary> /// <summary>
/// Adds an item to the collection. /// Adds an item to the collection.
/// </summary> /// </summary>
@ -433,10 +441,10 @@ namespace Perspex.Collections
/// <param name="index">The starting index.</param> /// <param name="index">The starting index.</param>
private void NotifyAdd(IList t, int index) private void NotifyAdd(IList t, int index)
{ {
if (CollectionChanged != null) if (_collectionChanged != null)
{ {
var e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, t, index); var e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, t, index);
CollectionChanged(this, e); _collectionChanged(this, e);
} }
NotifyCountChanged(); NotifyCountChanged();
@ -458,10 +466,10 @@ namespace Perspex.Collections
/// <param name="index">The starting index.</param> /// <param name="index">The starting index.</param>
private void NotifyRemove(IList t, int index) private void NotifyRemove(IList t, int index)
{ {
if (CollectionChanged != null) if (_collectionChanged != null)
{ {
var e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, t, index); var e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, t, index);
CollectionChanged(this, e); _collectionChanged(this, e);
} }
NotifyCountChanged(); NotifyCountChanged();
@ -473,7 +481,7 @@ namespace Perspex.Collections
/// <param name="t">The items that were removed.</param> /// <param name="t">The items that were removed.</param>
private void NotifyReset(IList t) private void NotifyReset(IList t)
{ {
if (CollectionChanged != null) if (_collectionChanged != null)
{ {
NotifyCollectionChangedEventArgs e; NotifyCollectionChangedEventArgs e;
@ -481,7 +489,7 @@ namespace Perspex.Collections
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset) : new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset) :
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, t, 0); new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, t, 0);
CollectionChanged(this, e); _collectionChanged(this, e);
} }
NotifyCountChanged(); NotifyCountChanged();

Loading…
Cancel
Save