委托和事件处理问题
在我谷歌了这么多之后,我仍然无法理解委托和事件是如何工作的。
我想要实现的是: 类1将播放动画,当动画结束时,它将把委托/事件传递给类2,以便类2在动画结束时执行一些操作。
我想知道如何对上述行为进行编码?
After I google so much and I still cant understand how does the delegate and event works.
What I want to achieve is:
Class 1 will be playing animation, when the animation END, it will pass the delegate/event to the Class 2 so that Class 2 will do something on the animation END.
I wonder how can I code the above behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
非常基本的事件实现...
这个链接 提供了一个关于如何创建事件的快速示例。它是针对 VS 2003 的,但仍然适用。
Very basic event implementation...
This link provides a good quick example on how to create events. It's targeted for VS 2003, but is still applicable.
将其视为发布者(公开事件的类)和订阅者(向事件注册处理程序的类)。
但是,在您提到的场景中,您不能直接从 Class1 中调用 Class2 的 DoSomething 吗?
Think of it as a Publisher (the class exposing an event) and Subscriber (the class registering an handler to event).
Though, in the scenario you mentioned, can you not call the DoSomething of Class2 directly from within Class1?
这里有两个类。我假设您正在谈论上面的实例。
然后,如果你有一个获胜形式,你会得到:
输出将是:
Here are two classes. I assume you're talking about instances above.
Then, if you had a win form you'd have:
And the output would be:
因此,
Class1
将公开一个事件,Class2
将向该事件附加一个委托。当动画结束时,Class1
将引发该事件。这是你需要的吗?So
Class1
will expose an event, to whichClass2
will attach a delegate. When the animation endsClass1
will raise the event. Is that what you need?