1

我有如下所示的代码。我遇到的问题是这部分代码给出了编译错误。

Changed(this, new ListChangedEventArgs(Operation.Add, e.Value, 1));

错误 1 ​​使用泛型类型“CustomDatastructures.Core.ListChangedEventArgs”需要 1 个类型参数

第二个问题是如何调用 onChanged 方法。


//public delegate void ListChanged<TEventArgs>(object sender, TEventArgs e);
public delegate void BeforeListChanged<TEventArgs>(object sender, TEventArgs e);
public delegate void ListChanged<TEventArgs>(object sender, TEventArgs e);

// Make this class generic by adding a type-parameter to the class
public class ObservableList<T> : IEnumerable<T>
{
    // Declare an private variabel to work as 
    // the internal data storage for the list
    List<T> observerList = new List<T>();

    public event ListChanged<ListChangedEventArgs<T>> Changed;
    //public event BeforeListChanged<T> BeforeChanged;

    protected virtual void OnChanged(object sender, Operation op, T value, int count)
    {
        if (Changed != null)
            Changed(this, new ListChangedEventArgs(op, value, count));
    }



    /// <summary>
    /// Add and object to the list
    /// </summary>
    /// <param name="item">An object</param>
    public void Add(T item)
    {
        observerList.Add(item);
        OnChanged(this, Operation.Add, item, 1);
    }

这是 ListChangedEvent 类的定义

public class ListChangedEventArgs<T> : EventArgs
{
public int Count { get; }
public Operation Operation { get; }
public T Value { get; }
public ListChangedEventArgs(Operation operation, T value, int count)
}
4

1 回答 1

2

您正在声明您ObservableCollection的 type T。这个集合有一个事件ListChanged<T>。但是您将此事件称为ListChanged<ListChangedEventArgs<T>>. 所以你必须改变你的活动声明:

public event ListChanged<ListChangedEventArgs<T>> Changed;
protected virtual void OnChanged(object sender, ListChangedEventArgs<T> e)
{
    if (Changed != null)
        Changed(this, e);
}
于 2013-10-15T07:56:56.923 回答