简体   繁体   English

活动和代表

[英]Events and Delegates

Button.Click += new RoutedEventHandler(_click);

private void _click(object sender EventArgs e) 
{
   //... 
} 

In the code above, we're instantiating the RoutedEventHandler type, which is a delegate, with the Button.Click event. 在上面的代码中,我们使用Button.Click事件实例化RoutedEventHandler类型,它是一个委托。 But the event is an abstracted delegate by itself, isn't it? 但事件本身就是一个抽象的代表,不是吗? I don't understand the difference between this and just instantiating the RoutedEventHandler to a variable, and then adding variables to the instance's invocation list. 我不明白它与仅将RoutedEventHandler实例化为变量,然后将变量添加到实例的调用列表之间的区别。 Am I making this too hard? 我这太难了吗? How do all of the delegates involved here work? 这里涉及的所有代表如何运作?

Edit: so my main concern is just trying to bridge the gap between what I know about delegates and what I know about events. 编辑:所以我主要关心的是试图弥合我对代表的了解与我对事件的了解之间的差距。 I know an event is a delegate wrapped in another layer of abstraction. 我知道一个事件是一个包含在另一个抽象层中的委托。 So when you assign another delegate to its invocation list using the += operator, you're just assigning a delegate to another delegate, correct? 因此,当您使用+ =运算符将另一个委托分配给其调用列表时,您只是将委托分配给另一个委托,对吗? But in the code I wrote above, you're not actually instantiating the RoutedEventHandler class, so I'm confused about how you're actually passing it into the invocation list of the Button.Click event. 但是在我上面编写的代码中,你实际上并没有实例化RoutedEventHandler类,所以我对你如何将它实际传递到Button.Click事件的调用列表感到困惑。 I also get confused because it seems like everything is actually pointing to something else with delegates and events, and the references get complicated. 我也感到困惑,因为看起来所有东西实际上都指向委托和事件的其他东西,并且引用变得复杂。

You can think of events as wrappers to a collection of delegates (with some syntactic sugar for adding / removing members). 您可以将事件视为代理集合的包装(具有添加/删除成员的一些语法糖)。 Events deal with multicasting the call to multiple delegates, you can add custom logic to allow (or not) a delegate to be added (the same way you can wrap a field in a property and add some logic on the getter / setter for the property). 事件处理组播对多个委托的调用,您可以添加自定义逻辑以允许(或不允许)添加委托(与在属性中包装字段并在属性的getter / setter上添加一些逻辑的方式相同) )。 Having an event in a class "advertises" to the world that they can safely add handlers (which are implemented as delegates) to receive said events - and that allows for things such as design-time integration with IDEs such as Visual Studio. 让一个类中的事件向全世界“广告”他们可以安全地添加处理程序(实现为委托)来接收所述事件 - 并允许诸如与Visual Studio等IDE之类的设计时集成之类的事情。

When you use a delegate in the context of an event the compiler will generate both a provide backing field for the delegate and a add/remove public property for subscribers to attach to the event. 当您在事件的上下文中使用委托时,编译器将为委托生成提供支持字段,并为订阅者生成添加/删除公共属性以附加到事件。 You could just use the delegate as an event as you describe however you will not be able to limit the subscribers to just += and -= 您可以将委托作为事件使用,但是您将无法将订阅者限制为+ =和 - =

private EventHandler _backingDelegate;
public event EventHandler Click {
   add {
         _backingDelegate += value;
   } 
   remove {
         _backingDelegate -= value;
   }
}

Probably this answer will help you.He has explained it in good detail:- 这个答案可能会对你有所帮助。他已经详细解释了这个问题: -

Events 活动

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

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