这行代码是什么?

发布于 2024-10-29 19:34:58 字数 166 浏览 3 评论 0原文

我经常在代码中看到一行代码,我知道它对事件做了一些事情,但不清楚它做了什么。

我是否可以将表单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔嚣张 2024-11-05 19:34:59

是的,这显然与事件有关。

.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 the Form.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 the Form.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.

书信已泛黄 2024-11-05 19:34:59

此行的含义是您将方法 CGForm_Load() 订阅到 this.CGForm.Load 事件。方法CGForm_Load()事件处理程序回调

运行此行后,每次引发事件时(在本例中 - 每次加载表单时),都会自动调用 CGForm_Load() 。

为了取消订阅回调,请像这样使用-=

this.CGForm.Load -= new EventHandler(CGForm_Load)

取消订阅事件回调后,下次引发事件时(如果再次加载表单)回调将不再被调用。


我发现以下比喻很有帮助:事件就像电源插座,回调就像电源插头。订阅就像将插头插入插座,取消订阅就像将其拔出。当存在连接时,该事件的所有调用都会触发回调。

What this line means is that you subscribe the method CGForm_Load() to the this.CGForm.Load event. The method CGForm_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:

this.CGForm.Load -= new EventHandler(CGForm_Load)

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.

非要怀念 2024-11-05 19:34:59

它只是将 GCForm_Load 方法添加到 CGForm 上的 Load 事件中。当 CGForm 加载时,会在幕后执行如下代码。每当这种情况发生时,所有事件订阅者(在本例中,此处订阅的方法为 CGForm_Load)都将被调用。

<pseudocode>
class CGForm
{
  public EventHandler<FormLoadedEventArgs> Load;

  private void SomeMethodThatLoadsTheForm()
  {
    LoadForm();
    var loadHandlers = Load;
    if (loadHandlers != null)
    {
       loadHandlers(new FormLoadedEventArgs(...));
    }
  }
}
</pseudocode>

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.

<pseudocode>
class CGForm
{
  public EventHandler<FormLoadedEventArgs> Load;

  private void SomeMethodThatLoadsTheForm()
  {
    LoadForm();
    var loadHandlers = Load;
    if (loadHandlers != null)
    {
       loadHandlers(new FormLoadedEventArgs(...));
    }
  }
}
</pseudocode>
我是有多爱你 2024-11-05 19:34:59

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 delegate CGForm_Load to the Load event of the CGForm 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文