在类中引发事件,并在另一个类上使用处理程序 [英] Raising an event inside a class and use an handler on another class

查看:130
本文介绍了在类中引发事件,并在另一个类上使用处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个类库,我在Windows窗体程序中使用它。
我想在程序中处理这个库的一个事件。
我使用这个代码:

I'm making a Class Library and I'm using it inside a Windows Form Program. I would like to handle an event of this library inside the program. I'm using this code :

在库中的类:

public class KSEDataServIO
{
    public delegate void IsReadyForUseEventHandler(object sender, IsReadyForUseEventArgs e);
    public event IsReadyForUseEventHandler IsReadyForUse;
    public KSEDataServIO(){
       EvArg = new IsReadyForUseEventArgs("AuthOkay");
       IsReadyForUse(this, EvArg); //This is where i get the issue.
    }
}



And, In the Window Form I'm doing this :

private void button1_Click(object sender, EventArgs e) {
            KSEDataServIO con = new KSEDataServIO();
            con.IsReadyForUse += new KSEDataServIO.IsReadyForUseEventHandler(con_IsReadyForUse);
}

void con_IsReadyForUse(object sender, IsReadyForUseEventArgs e)
{
     MessageBox.Show(e.Etat);
}



我得到了一个N​​ullReferenceException的行IsReadyForUse(this,EvArg)里面的类库。
任何想法?

I got a NullReferenceException to the line 'IsReadyForUse(this, EvArg);' inside the class library. Any idea ?

推荐答案

你的问题是你在 KSEDataServIO 。在这一点上没有订阅该事件处理程序,因此它引发了一个空引用异常。

Your problem is that you raise the event inside the constructor of KSEDataServIO. At that point nothing has subscribed to that event handler and therefore it raises a null reference exception.

所以一件事是正确地提高这个模式通常的事件处理程序used:

So one thing is to properly raise the event handler for which this pattern is commonly used:

public delegate void IsReadyForUseEventHandler(object sender, IsReadyForUseEventArgs e);
public event IsReadyForUseEventHandler IsReadyForUse;

void OnIsReadyForUse(IsReadyForUseEventArgs e)
{
    var handler = IsReadyForUse;
    if (handler != null)
    {
        handler(this, e);
    }
}

然后使用它来引发事件: / p>

Then use it like this to raise the event:

OnIsReadyForUse(new IsReadyForUseEventArgs("AuthOkay"))

其次在构造函数中提高事件没有什么意义,因为没有什么可能已经订阅了处理程序(除非你传递一个处理程序作为构造函数参数)。

Secondly raising the event inside your constructor doesn't make much sense as nothing could possibly have yet subscribed to the handler (unless you pass a handler as constructor parameter). You will need to find another trigger where you can raise the event later on.

此外,您还应该公开一个 IsReady 属性。因此,如果用户后来可以查询对象,如果它已经准备好了。如果 IsReady 事件在您开始使用该对象时已经引发,那么您可能会错过该事件。

Also you should expose a IsReady property in your class. So if a user comes along later it can query the object if it is already ready. If the IsReady event was already raised when you start using the object somewhere then you might miss the event otherwise.

修改:如果您真的想要这样做,您可以向构造函数传递一个处理程序:

Edit: You could pass a handler to the constructor if you really wanted to do this:

public KSEDataServIO(IsReadyForUseEventHandler handler)
{
   IsReadyForUse += handler;
   OnIsReadyForUse(new IsReadyForUseEventArgs("AuthOkay")); // see pattern above
}

但是由于你的事件提供这个作为发送者,在它完成执行整个构造函数之前传递一个对象的引用,可能导致难以跟踪的问题。如果你唯一想要提升事件的地方是构造函数的结束,那么你根本不需要它。

However as your event provides this as the sender you pass a reference to an object before it has finished executing the whole constructor potentially leading to problems which are hard to track down. If the only place where you are ever going to raise the event is the end of the constructor then you don't really need it.

这篇关于在类中引发事件,并在另一个类上使用处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆