16

我创建了一个继承List的 Class EventList,它在每次添加、插入或删除某些内容时触发一个事件:

public class EventList<T> : List<T>
{
    public event ListChangedEventDelegate ListChanged;
    public delegate void ListChangedEventDelegate();

    public new void Add(T item)
    {
        base.Add(item);
        if (ListChanged != null
            && ListChanged.GetInvocationList().Any())
        {
            ListChanged();
        }
    }
    ...
}

目前,我将它用作这样的属性:

public EventList List
{
    get { return m_List; }
    set
    {
        m_List.ListChanged -= List_ListChanged;

        m_List = value;

        m_List.ListChanged += List_ListChanged;
        List_ListChanged();
    }
}

现在我的问题是,如果一个新对象被引用或阻止它,我可以以某种方式处理它,所以我不必在设置器中进行事件连接吗?

当然,我可以将属性更改为“私有集”,但我也希望能够将类用作变量。

4

5 回答 5

26

您很少在类中创建集合类的新实例。实例化一次并清除它,而不是创建一个新列表。(并使用 ObservableCollection,因为它已经继承了INotifyCollectionChanged接口)

private readonly ObservableCollection<T> list;
public ctor() {
    list = new ObservableCollection<T>();
    list.CollectionChanged += listChanged;
}

public ObservableCollection<T> List { get { return list; } }

public void Clear() { list.Clear(); }

private void listChanged(object sender, NotifyCollectionChangedEventArgs args) {
   // list changed
}

这样,您只需连接一次事件,并且可以通过调用 clear 方法来“重置”它,而不是检查属性的 set 访问器中的前一个列表是否为 null 或相等。


通过 C#6 中的更改,您可以从没有支持字段的构造函数分配 get 属性(支持字段是隐式的)

所以上面的代码可以简化为

public ctor() {
    List = new ObservableCollection<T>();
    List.CollectionChanged += OnListChanged;
}

public ObservableCollection<T> List { get; }

public void Clear()
{
    List.Clear();
}

private void OnListChanged(object sender, NotifyCollectionChangedEventArgs args)
{
   // react to list changed
}
于 2012-10-08T15:56:53.720 回答
12

ObservableCollection 是一个带有 CollectionChanged 事件的列表

ObservableCollection.CollectionChanged 事件

有关如何连接事件处理程序,请参阅 Patrick 的回答。+1

不确定您在寻找什么,但我将它用于一个集合,其中包含一个在添加、删除和更改时触发的事件。

public class ObservableCollection<T>: INotifyPropertyChanged
{
    private BindingList<T> ts = new BindingList<T>();

    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged( String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public BindingList<T> Ts
    {
        get { return ts; }
        set
        {
            if (value != ts)
            {
                Ts = value;
                if (Ts != null)
                {
                    ts.ListChanged += delegate(object sender, ListChangedEventArgs args)
                    {
                        OnListChanged(this);
                    };
                }
                NotifyPropertyChanged("Ts");
            }
        }
    }

    private static void OnListChanged(ObservableCollection<T> vm)
    {
        // this will fire on add, remove, and change
        // if want to prevent an insert this in not the right spot for that 
        // the OPs use of word prevent is not clear 
        // -1 don't be a hater
        vm.NotifyPropertyChanged("Ts");
    }

    public ObservableCollection()
    {
        ts.ListChanged += delegate(object sender, ListChangedEventArgs args)
        {
            OnListChanged(this);
        };
    }
}
于 2012-10-08T15:48:24.820 回答
5

如果您不想或无法转换为可观察集合,请尝试以下操作:

public class EventList<T> : IList<T> /* NOTE: Changed your List<T> to IList<T> */
{
  private List<T> list; // initialize this in your constructor.
  public event ListChangedEventDelegate ListChanged;
  public delegate void ListChangedEventDelegate();

  private void notify()
  {
      if (ListChanged != null
          && ListChanged.GetInvocationList().Any())
      {
        ListChanged();
      }
  }

  public new void Add(T item)
  {
      list.Add(item);
      notify();
  }

  public List<T> Items {
    get { return list; } 
    set {
      list = value; 
      notify();
    }
  }
  ...
}

现在,对于您的财产,您应该能够将代码简化为:

public EventList List
{
  get { return m_List.Items; }
  set
  {
      //m_List.ListChanged -= List_ListChanged;

      m_List.Items = value;

      //m_List.ListChanged += List_ListChanged;
      //List_ListChanged();
  }
}

为什么?在 EventList.Items 中设置任何内容都会调用您的私人notify()例程。

于 2012-10-08T16:01:46.560 回答
0

当有人从 IList.add(object) 调用通用方法时,我有一个解决方案。以便您也收到通知。

using System;
using System.Collections;
using System.Collections.Generic;

namespace YourNamespace
{
    public class ObjectDoesNotMatchTargetBaseTypeException : Exception
    {
        public ObjectDoesNotMatchTargetBaseTypeException(Type targetType, object actualObject)
            : base(string.Format("Expected base type ({0}) does not match actual objects type ({1}).",
                targetType, actualObject.GetType()))
        {
        }
    }

    /// <summary>
    /// Allows you to react, when items were added or removed to a generic List.
    /// </summary>
    public abstract class NoisyList<TItemType> : List<TItemType>, IList
    {
        #region Public Methods
        /******************************************/
        int IList.Add(object item)
        {
            CheckTargetType(item);
            Add((TItemType)item);
            return Count - 1;
        }

        void IList.Remove(object item)
        {
            CheckTargetType(item);
            Remove((TItemType)item);
        }

        public new void Add(TItemType item)
        {
            base.Add(item);
            OnItemAdded(item);
        }

        public new bool Remove(TItemType item)
        {
            var result = base.Remove(item);
            OnItemRemoved(item);
            return result;
        }
        #endregion

        # region Private Methods
        /******************************************/
        private static void CheckTargetType(object item)
        {
            var targetType = typeof(TItemType);
            if (item.GetType().IsSubclassOf(targetType))
                throw new ObjectDoesNotMatchTargetBaseTypeException(targetType, item);
        }
        #endregion

        #region Abstract Methods
        /******************************************/
        protected abstract void OnItemAdded(TItemType addedItem);

        protected abstract void OnItemRemoved(TItemType removedItem);
        #endregion
    }
}
于 2015-05-19T13:05:48.653 回答
0

如果 ObservableCollection 不是您的解决方案,您可以尝试:

A) 实现一个自定义 EventArgs,它在触发事件时将包含新的 Count 属性。

public class ChangeListCountEventArgs : EventArgs
{
    public int NewCount
    {
        get;
        set;
    }

    public ChangeListCountEventArgs(int newCount)
    {
        NewCount = newCount;
    }
}

B)实现一个从 List 继承的自定义 List 并根据您的需要重新定义 Count 属性和构造函数:

public class CustomList<T> : List<T>
{
    public event EventHandler<ChangeListCountEventArgs> ListCountChanged;

    public new int Count
    {
        get
        {
            ListCountChanged?.Invoke(this, new ChangeListCountEventArgs(base.Count));
            return base.Count;
        }
    }

    public CustomList()
    { }

    public CustomList(List<T> list) : base(list)
    { }

    public CustomList(CustomList<T> list) : base(list)
    { }
}

C) 最后订阅您的活动:

var myList = new CustomList<YourObject>();
myList.ListCountChanged += (obj, e) => 
{
    // get the count thanks to e.NewCount
};
于 2018-01-03T13:48:03.260 回答