这行代码是什么?
我经常在代码中看到一行代码,我知道它对事件做了一些事情,但不清楚它做了什么。
我是否可以将表单 A 的加载事件附加到表单 B 中,或者它有什么好处?
this.CGForm .Load +=new EventHandler(CGForm_Load);
Often in code I see a line of code, I have an idea it do something about event but not clearly know what it does.
Is it possible that I can attach form A's load event in Form B with this, or what is its benefit?
this.CGForm .Load +=new EventHandler(CGForm_Load);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,这显然与事件有关。
.Load
在这种情况下Form.Load
事件。+=
运算符向事件添加一个事件处理程序。一个事件可以同时有多个事件处理程序。事件处理程序只是常规方法,可以位于代码库中的任何位置。当事件触发时,所有订阅的方法将被依次调用。我认为没有特别好的理由让 FormA 侦听 FormB 的
Load
事件,但其他事件可能更有趣,例如Form.Closed
事件。这是让 FormA 对 FormB 中的更改做出反应的一种方式。编辑
请注意,这会导致 FormA 保留对 FormB 的引用,并且在 FormA 释放对 FormB 的引用之前,FormB 不会被垃圾回收(使用
this.CGForm .Load -=new EventHandler(CGForm_Load);
,请注意-=
)这是 .NET 中内存泄漏的常见原因。从其他形式订阅事件是一种“代码味道”,可能是代码的潜在结构问题。在某些情况下这是必需的,但如果你到处都有它,你的代码将难以理解和维护。
Yes, it clearly has to do with events.
.Load
in this case the theForm.Load
event. The+=
operator adds one event handler to the event. An event can have many event handlers at the same time. Event handlers are just regular method that can be anywhere in your code base. When the event fires all subscribed methods will be called, one after the other.I see no particular good reason to have FormA listen to the
Load
event of FormB, but other events might be more interesting, like theForm.Closed
event. This is a way to have FormA react to changes in FormB.Edit
Note that this causes FormA to hold a reference to FormB and FormB won't be garbage collected until FormA releases the reference to FormB (with
this.CGForm .Load -=new EventHandler(CGForm_Load);
, note the-=
) this is a common cause for memory leaks in .NET.Subscribing to events from other forms is a "code smell" that can be a potential structure problem with your code. In some cases it is required, but if you have it all over the place your code will be hard to understand and maintain.
此行的含义是您将方法
CGForm_Load()
订阅到this.CGForm.Load
事件。方法CGForm_Load()
是事件处理程序或回调。运行此行后,每次引发事件时(在本例中 - 每次加载表单时),都会自动调用 CGForm_Load() 。
为了取消订阅回调,请像这样使用
-=
:取消订阅事件回调后,下次引发事件时(如果再次加载表单)回调将不再被调用。
我发现以下比喻很有帮助:事件就像电源插座,回调就像电源插头。订阅就像将插头插入插座,取消订阅就像将其拔出。当存在连接时,该事件的所有调用都会触发回调。
What this line means is that you subscribe the method
CGForm_Load()
to thethis.CGForm.Load
event. The methodCGForm_Load()
is the event handler or the callback.After you run this line, every time the event is raised (in this case - every time the form is loaded),
CGForm_Load()
will be called automatically.In order to unsubscribe a callback, use
-=
like this:Once an event callback is unsubscribed, the next time the event is raised (if the form is loaded again) the callback will no longer be called.
I find the following metaphor helpful: An event is like a power outlet, and callbacks are like power plugs. Subscribing is like connecting the plug to the outlet, and unsubscribing is like pulling it out. While there is a connection, all invocations of the event trigger the callback.
它只是将 GCForm_Load 方法添加到 CGForm 上的 Load 事件中。当 CGForm 加载时,会在幕后执行如下代码。每当这种情况发生时,所有事件订阅者(在本例中,此处订阅的方法为 CGForm_Load)都将被调用。
It simply adds the GCForm_Load method to the Load event on the CGForm. Under the hood when the CGForm is loaded code like the following will execute. Whenever this happens all event subscribers (in this case the method being subscribed here as CGForm_Load) will be invoked.
this.CGForm .Load +=new EventHandler(CGForm_Load);
将事件处理程序委托CGForm_Load
订阅到CGForm 的
Load
事件对象。有关订阅事件的文档可以在 http://msdn.microsoft.com/en-us/library/ms366768(v=vs.80).aspx" rel="nofollow">http:// /msdn.microsoft.com/en-us/library/ms366768(v=vs.80).aspx 。
有关 .NET 中事件如何工作的信息索引可以在 http://msdn.microsoft.com/en-US/library/awbftdfh(v=VS.80).aspx
this.CGForm .Load +=new EventHandler(CGForm_Load);
subscribes the event handler delegateCGForm_Load
to theLoad
event of theCGForm
object.Documentation about subscribing to events can be found at http://msdn.microsoft.com/en-us/library/ms366768(v=vs.80).aspx .
And index of information about how events work in .NET can be found at http://msdn.microsoft.com/en-US/library/awbftdfh(v=VS.80).aspx