“简单/快速"事件 [英] "Simple / Fast" Event

查看:106
本文介绍了“简单/快速"事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是找到了一段代码(不足以找到它),我想知道我该怎么做,因为在我看来,这比正常事件要好得多

i just found a piece(not enought to find it out) of code and i''m wondering how i can do this, because it''s much better than a normal event in my case

console.BindCommand("!", delegate(GameTime gameTime, string[] args)
            {
                console.WriteLine(args[0]);
            });


一个简单的外观示例.
因此,如果我的班级检测到用户写了!",它将被运行.

感谢您的帮助:)


a simple example of how it looks.
so this will be runned if my class detects that the user wrote "!"

thanks for the help :)

推荐答案

这就是您所需要的.委托是一个匿名方法.因此,您可以使用此语法创建内联事件.真正的优点是,创建此方法的方法中的任何变量对于委托都是可见的.如果您需要更多帮助,请使用Google匿名方法.

我应该补充一点,它的运行速度不会比外部定义的委托方法快或慢.
It''s all you need. The delegate is an anonymous method. So, you use this syntax to create inline events. The real advantage is, any variables in the method that creates this, are visible to the delegate. Google anonymous methods if you need more help.

I should add, this runs no faster or slower than an externally defined delegate method.


Christian是绝对正确的,并且在.NET 4.0中使用Lambda格式可以更简单地定义事件:
Hi, Christian is absolutely right, and an event definition can be even simpler using Lambda form in .NET 4.0:
//''SomeControl'' stands for any control that exposes a ''KeyDown'' event
SomeControl.KeyDown += (sndr,eArgs)=>{MessageBox.Show(eArgs.KeyCode.ToString());};

在此示例中,您在通过Lambda同时附加事件的同时定义了事件.

建议您阅读Jon Skeet的"C#in Depth"(曼宁出版社)以获取对事件领域中所有进展的很好解释,.NET C#中的代表.

In this example, you define the event while attaching it at the same time via Lambda.

Suggest you read Jon Skeet''s "C# in Depth" (Manning Press) to get a great explanation of all the developments in the area of events, delegates in .NET C#.


post -脚本:

Anooop Madhusudanan,出色的"AmazedSaint's Tech #Journal"博客[ ^ ],已经在线发表了一篇出色的论文集,在回顾.NET中事件"的历史方面做得很出色:

http://www.scribd.com/doc/53728207/Revisting-C-V1-0

随着新版本的.NET引入Lambda等,他非常清楚地展示了编写事件的不同方式.

最好,比尔
post-script:

Anooop Madhusudanan, author of the excellent ''AmazedSaint''s Tech #Journal'' blog[^], has published an excellent collection of his essays on-line, that does a great job of reviewing the history of ''events'' in .NET:

http://www.scribd.com/doc/53728207/Revisting-C-V1-0

He demonstrates very clearly different ways of writing events as newer versions of .NET has introduced Lambdas, etc.

best, Bill


这篇关于“简单/快速"事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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