简体   繁体   English

C# 使用事件处理程序创建事件列表,以向事件列表添加或删除处理程序

[英]C# Create a list of events with an event handler to add or remove the handler to the event list

In an application, I have a rather large list of events and their assigned handlers such as:在一个应用程序中,我有一个相当大的事件列表及其分配的处理程序,例如:

...
Resize += FormMain_Resize;
ResizeEnd += FormMain_ResizeEnd;
...

What I am wanting to do is add all these items into a list so that I can iterate through them to unassign (turn them off), do a bunch of work, then iterate through the list again to reassign the event handlers to the events.我想要做的是将所有这些项目添加到列表中,以便我可以遍历它们以取消分配(关闭它们),做一堆工作,然后再次遍历列表以将事件处理程序重新分配给事件。

To date I've not found any relevant information dealing with creating an actual list and their assigned event handlers.迄今为止,我还没有找到任何有关创建实际列表及其分配的事件处理程序的相关信息。 Is this a thing I should not be attempting?这是我不应该尝试的事情吗?

I created a class as follows:我创建了一个 class 如下:

public class EventWHandler
{
    public EventWHandler(EventHandler e, EventHandler m)
    {
        EventItem = e;
        Method = m;
    }

    public EventHandler EventItem { get; private set; }
    public EventHandler Method { get; private set; }

    public void Listen()
    {
        EventItem += Method;
    }

    public void UnListen()
    {
        EventItem -= Method;
    }
}

With the idea of creating a list of the above class with:有了创建上述 class 列表的想法:

list.Add(new EventWHandler(Resize, FormMain_Resize));

However, this will not work without some tweaking - as I was not able to make an object reference to the event (and used EventHandler for demonstration)但是,如果不进行一些调整,这将无法工作——因为我无法对事件进行 object 引用(并使用 EventHandler 进行演示)

Is there a better way to iterate through a specified list of events and their handlers?有没有更好的方法来遍历指定的事件列表及其处理程序?

(Note: I'm not wanting to iterate through ALL events nor ALL of an event's handlers - just a specified list.) (注意:我不想遍历所有事件,也不想遍历所有事件的处理程序 - 只是一个指定的列表。)

(Note: Using .net 4.7 - project constraints) (注意:使用 .net 4.7 - 项目约束)

I think you may want System.Collections.ObjectModel.ObservableCollection<EventHandler> and System.Delegate classes我想你可能想要System.Collections.ObjectModel.ObservableCollection<EventHandler>System.Delegate

You should use ObservableCollection<T> class to update the properties when the list is modified, added, removed, or replaced.当修改、添加、删除或替换列表时,您应该使用ObservableCollection<T> class 来更新属性。

I think it would work.我认为它会起作用。

using System.Collections.ObjectModel;
using System.Collections.Specialized;

public class EventWHandler : ObservableCollection<EventHandler>
{
    public EventHandler CombinedItem { get; private set; }

    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {;
        if (e.NewItems != null)
            CombinedItem = (EventHandler)Delegate.Combine(e.NewItems.Cast<EventHandler>().ToArray());
        base.OnCollectionChanged(e)
    }
}

In use, You don't need to unassign and then assign.在使用中,您不需要先取消分配再分配。 Adding the EventHandler will change CombinedItem immediately;添加 EventHandler 将立即更改 CombinedItem;

EventWHandler ewh = new EventWHandler()
{
    Resize,
    FormMain_Resize
};
ewh.Add(Native_Resize);
ewh.Remove(Native_Resize);

this.Resize += ewh.CombinedItem;

Also, read these references:另外,请阅读这些参考资料:
Delegate Methods , 委托方法
ObservableCollection , 可观察集合
NotifyCollectionChangedEventArgs NotifyCollectionChangedEventArgs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM