简体   繁体   English

在哪里注册C#活动?

[英]Where to register for C# events?

I am currently transitioning from VB to C# and am having some issues with regards to registering my interest in an event. 我目前正在从VB转换到C#,并且在注册我对事件的兴趣方面存在一些问题。

When using VB it was simply a case of specifying that a method Handles and event, often this was generated by using the object events list. 当使用VB时,它只是指定方法Handles和event的情况,通常这是通过使用对象事件列表生成的。 While I can easily use the Class.event += delegate in C# I am unsure where the best place is to place the code to do this. 虽然我可以在C#中轻松使用Class.event += delegate ,但我不确定放置代码的最佳位置。

Am I best placing it inside of the InitializeComponent() as per the generated code (say if you select the event in the from designer) or should I place it inside the constructor for better readability/maintenance. 我最好根据生成的代码将它放在InitializeComponent()中(例如,如果你在from designer中选择事件),或者我应该将它放在构造函数中以获得更好的可读性/维护。 If inside the constructor, should it be before or after the call to InitializeComponent() ? 如果在构造函数内部,它应该在调用InitializeComponent()之前还是之后?

When you are doing WinForm development (judging from InitializeComponent() function mentioned), usually you assign the handler using Visual Studio. 当您进行WinForm开发时(从提到的InitializeComponent()函数判断),通常使用Visual Studio分配处理程序。 You look up the properties of your control, click on the lightning icon to get the list of all events, find your event, and either double click on it (to create a new handler), or select existing handler from the list. 您查找控件的属性,单击闪电图标以获取所有事件的列表,找到您的事件,然后双击它(创建新的处理程序),或从列表中选择现有的处理程序。 Visual Studio will add the wiring of this in the generated code, so you don't have to worry about it. Visual Studio将在生成的代码中添加此接口,因此您不必担心它。

I always create a private method called Init() and place it there, and then call that method from the constructor or the Form_Load event handler. 我总是创建一个名为Init()的私有方法并将其放在那里,然后从构造函数或Form_Load事件处理程序中调用该方法。 It's semantically better, IMO, than doing it within the constructor proper. IMO在语义上比在构造函数中做得好。 And you don't want to place it within InitializeComponent() , because next time you change something in your designer it's likely to delete any manually-added code there. 并且您不希望将其放在InitializeComponent() ,因为下次您在设计器中更改某些内容时,可能会删除任何手动添加的代码。

Sometimes Visual Studio's designer can mess up the code, so adding the event handlers within InitializeComponent can create a headache, it would be better to do it something like this 有时,Visual Studio的设计人员可能会搞乱代码,因此在InitializeComponent添加事件处理程序会让人头疼,最好这样做。

public Form1(){
   InitializeComponent();
   WireUpEvents();
}

public void WireUpEvents(){
   this.fooEvent += new EventHandler(foo_handler);
   .... etc ....
}

And make sure that you remove the event handlers in the Form's Dispose function also... 并确保在Form的Dispose函数中删除事件处理程序...

public void UnWireEvents(){
   this.fooEvent -= new EventHandler(foo_handler);
   .... etc ....
}

As you design the form, Visual Studio will change the code within the InitializeComponent() method located in form.design.cs , so it is imperative that you do not manually edit this code.. 在设计表单时,Visual Studio将更改位于form.design.csInitializeComponent()方法中的form.design.cs ,因此您必须手动编辑此代码。

It depends, but most of the time, yes. 这取决于,但大多数时候,是的。

Use InitializeComponent when you want the event to be hooked for the entire duration of the Form (I'm assuming you're talking about Forms/UserControls/etc.). 如果希望在表单的整个持续时间内挂起事件,请使用InitializeComponent(我假设您正在讨论Forms / UserControls /等)。 In other cases, you'll want finer grained control of when the Event is handled. 在其他情况下,您需要更精细地控制何时处理事件。

Keep in mind that you'll want to unhook all of these events (using the -= syntax) when you're Disposing the Form, or no longer want to handle the event. 请记住,当您处置表单或不再想要处理事件时,您将要取消挂起所有这些事件(使用 - =语法)。 Keeping the event handler delegates attached is one of the most common managed memory leaks around. 保持事件处理程序委托附加是最常见的托管内存泄漏之一。

Do not manually add code to the InitializeComponent() method. 不要手动将代码添加到InitializeComponent()方法。 This method is code generated, so as soon as you change your form, any logic that you've added manually to this method will be wiped out. 此方法是代码生成的,因此只要更改表单,就会消除手动添加到此方法的任何逻辑。

I usually add a method to handle the Form's Load event and put my event registrations there. 我通常添加一个方法来处理Form的Load事件并将我的事件注册放在那里。

If you have the InitializeComponent() method you're using the designer so you can bind events directly in the designer if you like. 如果您使用的是InitializeComponent()方法,那么您可以根据需要直接在设计器中绑定事件。 To do this, click the lightning bolt icon in the properties window and you'll see a list of all the events for the selected object. 为此,请单击属性窗口中的闪电图标,您将看到所选对象的所有事件的列表。 You can just type the name of the event in there and it'll create the code for you. 您只需在其中键入事件的名称,它就会为您创建代码。

If you're not a fan of the designer, bind them after your InitializeComponent call and make sure you detach them when you're done (in Dispose() ). 如果您不是设计者的粉丝,请在InitializeComponent调用后绑定它们,并确保在完成后将它们分离(在Dispose() )。

2 ways of doing this. 这样做的两种方法。 You can either create you own method which you call in your Constructor which in turn creates the Event Handler, or you can just place them in your Constructor. 您可以创建自己的方法,在构造函数中调用该方法,然后创建事件处理程序,或者只需将它们放在构造函数中即可。 Probably a good idea to remove the Event Handlers in your Finalizer/Destructor code. 删除Finalizer / Destructor代码中的事件处理程序可能是个好主意。

I would place it after InitializeComponent, since you might be registering events against a child control/object, like a button, and you will want to be sure the object has been created already. 我会将它放在InitializeComponent之后,因为您可能正在针对子控件/对象注册事件,比如按钮,并且您将希望确保该对象已经创建。

There will be cases where you wire up to events dynamically/conditionally in other places, such as in response to some other event. 在某些情况下,您可以在其他位置动态/有条件地连接事件,例如响应其他某些事件。

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

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