何我在构造函数中引发事件?

时间:2017-07-17 14:43:54

标签: c# events eventhandler

我有一个类,其子类初始化并声明ObservableCollections。我想在基类中收集子集的集合,稍后在泛型方法中使用,所以我编写了一个事件处理程序,它在ObservableCollection的子节点的构造函数中引发,并在我的BaseView类中添加了处理程序。小记:很多子视图已经存在,我想在不改变代码的情况下实现它。我的ObservableCollection孩子看起来像这样:

public class ObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
{
    public ObservableCollection()
        :base()
    {
        OnConstructorCalled(new ConstructorCalledEventArgs(this));
    } 

    public static event ConstructorCalledEventHandler ConstructorCalled;
    protected static void OnConstructorCalled(ConstructorCalledEventArgs e) => ConstructorCalled?.Invoke(e.Sender, e);
}

public class ConstructorCalledEventArgs : EventArgs
{
    public object Sender;
    public ConstructorCalledEventArgs(object sender)
    {
        Sender = sender;
    }
}

public delegate void ConstructorCalledEventHandler(object sender, ConstructorCalledEventArgs e);

我的BaseView看起来像这样

    protected ObservableCollection<ObservableCollection<TObject>> ObservableCollectionCollector = new ObservableCollection<ObservableCollection<TObject>>();
    protected BaseView()
    {
        ObservableCollection<TObject>.ConstructorCalled += OnCollectionDeclared;
    }

    private void OnCollectionDeclared(object sender, ConstructorCalledEventArgs e)
    {
        if (sender is ObservableCollection<TObject>)
        {
            ObservableCollectionCollector.Add(sender as ObservableCollection<TObject>);
        }
    }

我现在的问题是,事件没有提出,我不知道为什么......

修改

我现在选择了一种不同的方法,因为我遇到了泛型类及其事件处理程序的一些问题。我创建了一个新的静态类,它现在处理Views和ObservableCollections的构造函数调用。它在我手中,我期待只有一种类型的ObservableCollection类。现在我的代码如下所示:

基本视角:

public class BaseView<TObject>
{
    public List<ObservableCollection<int>> ObservableCollectionCollector = new List<ObservableCollection<int>>();
    public BaseView()
    {
        ObservableCollectionCollector = CollectionHandler.GetAndRemoveCollection();
    }
}

的ObservableCollection:

public class ObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
{
    public ObservableCollection()
        : base()
    {
        AddToCollectionHandler();
    }
    private void AddToCollectionHandler()
    {
        if (this is ObservableCollection<int>)
        {
            CollectionHandler.AddToCollection(this as ObservableCollection<int>);
        }
    }
}   

和我的CollectionHandler:

static class CollectionHandler
{
    private static List<ObservableCollection<int>> collection = new List<ObservableCollection<int>>();
    public static List<ObservableCollection<int>> GetAndRemoveCollection()
    {
        List<ObservableCollection<int>> col = collection;
        collection = new List<ObservableCollection<int>>();
        return col;
    }
    public static void AddToCollection(ObservableCollection<int> item) => collection.Add(item);
}

顺便说一句。子视图可能如下所示:

public class ChildView : BaseView<string>
{
    ObservableCollection<int> collection = new ObservableCollection<int>();
    public ChildView()
        : base()
    {
    }
}

1 个答案:

答案 0 :(得分:2)

您正在课堂上创建ObservableCollectionCollector个实例&#39;字段初始化程序。

字段初始化程序在类构造函数体之前运行,以便在添加事件处理程序之前运行。您只需在处理静态事件后创建实例。