4

I have a ton on controls on a form, and there is a specific time when I want to stop all of my events from being handled for the time being. Usually I just do something like this if I don't want certain events handled:

private bool myOpRunning = false;

private void OpFunction()
{
    myOpRunning = true;
    // do stuff
    myOpRunning = false;
}

private void someHandler(object sender, EventArgs e)
{
    if (myOpRunning) return;
    // otherwise, do things
}

But I have A LOT of handlers I need to update. Just curious if .NET has a quicker way than having to update each handler method.

4

3 回答 3

9

您必须创建自己的机制来执行此操作。不过还不错。考虑添加另一层抽象。例如,一个简单的类调用FilteredEventHandler检查 myOpRunning 的状态,或者调用真实的事件处理程序,或者抑制事件。该类看起来像这样:

public sealed class FilteredEventHandler
{
    private readonly Func<bool> supressEvent;
    private readonly EventHandler realEvent;

    public FilteredEventHandler(Func<bool> supressEvent, EventHandler eventToRaise)
    {
        this.supressEvent = supressEvent;
        this.realEvent = eventToRaise;
    }

    //Checks the "supress" flag and either call the real event handler, or skip it
    public void FakeEventHandler(object sender, EventArgs e)
    {
        if (!this.supressEvent())
        {
            this.realEvent(sender, e);
        }
    }
}

然后,当您连接事件时,请执行以下操作:

this.Control.WhateverEvent += new FilteredEventHandler(() => myOpRunning, RealEventHandler).FakeEventHandler;

WhateverEvent被提升时,它会调用FilteredEventHandler.FakeEventHandler method. 该方法将检查标志并调用或不调用真实的事件处理程序。这在逻辑上与您已经在做的几乎相同,但是检查 myOpRunning 标志的代码只在一个地方,而不是散布在您的代码中。

编辑以回答评论中的问题:

现在,这个例子有点不完整。完全取消订阅事件有点困难,因为您丢失了对已连接的 FilteredEventHandler 的引用。例如,您不能这样做:

this.Control.WhateverEvent += new FilteredEventHandler(() => myOpRunning, RealEventHandler).FakeEventHandler;
//Some other stuff. . .
this.Control.WhateverEvent -= new FilteredEventHandler(() => myOpRunning, RealEventHandler).FakeEventHandler; //Not gonna work!

因为你连接了一个代表并解开了一个完全不同的代表!当然,这两个委托都是 FakeEventHandler 方法,但这是一个实例方法,它们属于两个完全不同的 FilteredEventHandler 对象。

不知何故,您需要获取对您构造的第一个 FilteredEventHandler 的引用才能解钩。这样的事情会起作用,但它涉及跟踪一堆 FilteredEventHandler 对象,这可能并不比您尝试解决的原始问题更好:

FilteredEventHandler filter1 = new FilteredEventHandler(() => myOpRunning, RealEventHandler);
this.Control.WhateverEvent += filter1.FakeEventHandler;
//Code that does other stuff. . .
this.Control.WhateverEvent -= filter1.FakeEventHandler;

在这种情况下,我要做的是让 FilteredEventHandler.FakeEventHandler 方法将其“this”引用传递给 RealEventHandler。这涉及更改 RealEventHandler 的签名以采用另一个参数:

public void RealEventHandler(object sender, EventArgs e, FilteredEventHandler filter);

或将其更改为采用您创建的 EventArgs 子类,该子类包含对 FilteredEventHandler 的引用。这是更好的方法

public void RealEventHandler(object sender, FilteredEventArgs e);
//Also change the signature of the FilteredEventHandler constructor:
public FilteredEventHandler(Func<bool> supressEvent, EventHandler<FilteredEventArgs> eventToRaise)
{
  //. . .
}
//Finally, change the FakeEventHandler method to call the real event and pass a reference to itself
this.realEvent(sender, new FilteredEventArgs(e, this)); //Pass the original event args + a reference to this specific FilteredEventHandler

现在,被调用的 RealEventHandler 可以自行取消订阅,因为它引用了正确的 FilteredEventHandler 对象,该对象传递给了它的参数。

我最后的建议是不要这样做! Neolisk 在评论中指出了这一点。做这样复杂的事情表明设计存在问题。将来需要维护此代码的任何人(甚至您,令人惊讶!)都很难弄清楚所涉及的非标准管道。

通常,当您订阅事件时,您只做一次就忘记了——尤其是在 GUI 程序中。

于 2013-04-18T18:16:05.713 回答
1

你可以用反射来做到这一点......

public static void UnregisterAllEvents(object objectWithEvents)
{
    Type theType = objectWithEvents.GetType();

    //Even though the events are public, the FieldInfo associated with them is private
    foreach (System.Reflection.FieldInfo field in theType.GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance))
    {
        //eventInfo will be null if this is a normal field and not an event.
        System.Reflection.EventInfo eventInfo = theType.GetEvent(field.Name);
        if (eventInfo != null)
        {
            MulticastDelegate multicastDelegate = field.GetValue(objectWithEvents) as MulticastDelegate;
            if (multicastDelegate != null)
            {
                foreach (Delegate _delegate in multicastDelegate.GetInvocationList())
                {
                    eventInfo.RemoveEventHandler(objectWithEvents, _delegate);
                }
            }
        }
    }
}
于 2013-04-18T17:57:12.997 回答
0

您可以禁用放置所有这些控件的容器。例如,如果您将它们放入 aGroupBoxPanel简单地使用:groupbox.Enabled = false;panel.Enabled = false;。您还可以禁用表单From1.Enabled = false;并显示等待光标。您仍然可以将这些控件复制并粘贴到窗体以外的容器中。

于 2020-11-26T10:10:27.930 回答