自定义委托和内置的事件处理程序委托 [英] Custom delegate and the in-built eventhandler delegate

查看:82
本文介绍了自定义委托和内置的事件处理程序委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用简单的简洁术语向我解释使用自定义EventHandler委托和内置的EventHandler委托的概念吗?



在教科书中( c'4.0在一个坚果壳中由o''reilly)我正在阅读它有关于预定义的非通用EventHandler委托说:当事件不携带额外信息时,它可以使用它(EventHandler委托)。 />


...当一个事件没有提供额外信息时。这部分是什么意思?是处理程序方法无需处理或传递任何数据的时候吗?



还有:

我在不同的场景中看到过其中一个用于触发事件。保证使用一种类型而不是另一种类型的是什么?

a。 EventName(this,e)

b。 EventName(新对象(),e)

c。 EventName(这个,新的EventArgs())



如果你知道其他任何事情,请在你的解释中说谎。谢谢。

解决方案

标准的EventHandler只向处理事件的方法提供两个项目:导致事件被触发的对象(发件人参数)和普通基本EventArgs( e 参数)。你不需要提供更多,然后你可以使用Eventargs在课堂上创建一个简单,快速创建的活动,外界可以订阅:

< pre lang =c#> /// < 摘要 >
/ // 指示MyEvent发生的事件
/// < / summary >
public event EventHandler MyEvent;
/// < 摘要 >
/// 调用以向订阅者发出MyEvent发生的信号
/// < / summary >
/// < ; param name =e > < / param >
受保护 虚拟 void OnMyEvent(EventArgs e)
{
EventHandler eh = MyEvent;
if (eh!= null
{
eh( ,e);
}
}



如果你需要提供更多,那么你必须使用自己的(派生的)EventArgs自定义EventHandler:

  public   partial   class  MyForm:表单
{
public event EventHandler< ChangedArgs>改变;

private void 但是go_Click(对象发​​件人,EventArgs e)
{
EventHandler ch =已更改;
if (ch!= null
{
ch( this new ChangedArgs(tbData.Text));
}
}
}

public partial class ChangedArgs:EventArgs
{
public string strData;
public ChangedArgs( string str)
{
strData = str ;
}
}





回答你的另一个问题:

a)什么时候您希望将现有事件从类中的事件处理程序传递到事件的处理程序。例如,在Click事件中,您可以将按钮处理程序获取的EventArgs传递给类DataReady事件的处理程序。

b)当您不想通过实际的类时实例到事件处理程序,但需要传递某种形式的对象(或假装它来自它真正做的不同对象)。从来没有必要自己使用它!

c)当你只想将一个新的,干净的EventArgs传递给处理程序时(可能是因为你不认为编写它的人可以信任检查 null 值。你可能是对......:叹气:


Can someone please explain to me in simple succinct terms the concept of using a custom EventHandler delegate and the in-built EventHandler delegate?

In a text book(c# 4.0 in a nut shell by o''reilly) i was reading it had this to say about the predefined nongeneric EventHandler delegate: "it (EventHandler delegate) can be used when an event doesn''t carry extra information."

"...when an event doesn''t carry extra information." What does this part mean? Is it when the handler method has no need to process or pass-on any data?

also:
I have seen in different scenerio where one of the following is used to fire an event. What warrants one using one type than the other?
a. EventName(this, e)
b. EventName(new object(), e)
c. EventName(this, new EventArgs())

If you know of any other please be kind to bed in your explanation. Thanks.

解决方案

A standard EventHandler supplies only two items to the method handling the event: the object that caused the event to be fired (the sender parameter) and the "normal", basic EventArgs (the e parameter). It you don''t need to supply more than this, then you can use the Eventargs to create a simple, quick-to-create Event in your class that the outside world can subscribe to:

/// <summary>
/// Event to indicate MyEvent happened
/// </summary>
public event EventHandler MyEvent;
/// <summary>
/// Called to signal to subscribers that MyEvent happened
/// </summary>
/// <param name="e"></param>
protected virtual void OnMyEvent(EventArgs e)
    {
    EventHandler eh = MyEvent;
    if (eh != null)
        {
        eh(this, e);
        }
    }


If you need to provide more than this, then you have to use a Custom EventHandler with your own (derived) EventArgs:

public partial class MyForm : Form
    {
    public event EventHandler<ChangedArgs> Changed;

    private void butGo_Click(object sender, EventArgs e)
        {
       EventHandler ch = Changed;
       if (ch != null)
          {
          ch(this, new ChangedArgs(tbData.Text));
          }
        }
    }

public partial class ChangedArgs : EventArgs
    {
    public string strData;
    public ChangedArgs(string str)
        {
        strData = str;
        }
    }



In answer to your other question:
a) When you want to pass the existing events up from a event handler in your class to the handler for your event. For example, in a Click event, you might pass the EventArgs that your button handler gets to the handler for your classes "DataReady" event.
b) When you don''t want to pass the actual class instance to the event handler, but need to pass an object of some form (or pretend it came from a different object that it really does). Never had to use this myself!
c) When you just want to pass a new, clean EventArgs to the handler (probably because you don''t think the person writing it can be trusted to check for null values. And you are probably right... :sigh:


这篇关于自定义委托和内置的事件处理程序委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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