我可以问一下如何听一个活动吗? [英] May I ask that how to listen to an event?

查看:46
本文介绍了我可以问一下如何听一个活动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢你花一些时间来查看我的问题。我的问题是如何监听事件???

Hi, Thank you for spending some time to view my question. My question is how to listen an event???

推荐答案

这是如何:



拥有,例如,一个类

This is how:

Having, for example, a class
public class MyClass {

   public MyClass(/* ... */) {
      //...
   } 

   public event System.EventHandler<SomeEventArgs> SomeEvent; 

}

您可以拥有此类的实例,并将事件句柄添加到事件实例的调用列表,一次或多次:

you can have an instance of this class and add an event handle to the invocation list of the event instance, once or several times:

MyClass instance = MyClass(/* ... */);

//...

instance.SomeEvent += (sender, eventArgs) => {
    //... do something, optionally using sender or evenArgs
};

//...
// and, somewhere else, having a reference to the same instance:

instance.SomeEvent += (sender, eventArgs) => {
    DoSomethingElse(eventArgs);
};

其中 eventArgs 的类型为 SomeEventArgs ,派生自 System.EventArgs



请参阅:

http://msdn.microsoft.com/en-us/library/vstudio/awbftdfh.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library /edzehd2t%28v=vs.110%29.aspx [ ^ ]。



另见我过去的回答:

< a href =http://www.codeproject.com/Answers/593614/UserControlpluscustomplusevent#answer1> UserControl自定义事件 [ ^ ],

WPF:如何在自定义控件中使用事件 [ ^ ]。



上面讨论的所有内容都是完全适用于WPF事件。同时,WPF事件具有高级分类,具有相当高级的功能,远远超出当前主题,但这件事很有用:

http://msdn.microsoft.com/en-us/library/ms753115%28v=vs.110%29.aspx [ ^ ]。



-SA

where eventArgs is of the type SomeEventArgs, derived from System.EventArgs.

Please see:
http://msdn.microsoft.com/en-us/library/vstudio/awbftdfh.aspx[^],
http://msdn.microsoft.com/en-us/library/edzehd2t%28v=vs.110%29.aspx[^].

See also my past answers:
UserControl custom event[^],
WPF : How to Use Event in Custom Control[^].

Everything discussed above is fully applied to WPF events. At the same time, WPF events have advanced classification with quite advanced features which go well beyond the current topic, but this matter is good to know:
http://msdn.microsoft.com/en-us/library/ms753115%28v=vs.110%29.aspx[^].

—SA


你不听和事件 - 使用事件处理程序方法处理它。

易于操作:在设计器中,突出显示要处理事件的对象,然后查看属性窗格。

单击事件按钮 - 它看起来像一个小闪电。

双击要处理的事件名称,它将为您创建一个事件处理程序,并将其链接到对象。



还有其他方法:输入对象实例名称i在编辑器中,后跟一个'。'

键入事件的名称,然后按+ =并按两次TAB。
You don't "listen to" and event - you handle it with an Event Handler method.
Easy to do: in the designer, highlight the object you want to handle an event for, and look at the Properties pane.
Click the "Events" button - it looks like a little lightning bolt.
Double click the event name you want to handle and it will create an event handler for you, and link it to the object.

There are other ways: type the object instance name in the editor, followed by a '.'
Type the name of the Event, and then "+=" and press TAB twice.


这篇关于我可以问一下如何听一个活动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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