简体   繁体   English

C#类问题

[英]C# Class Question

I have a Winform singleton class that contains a number of buttons. 我有一个Winform单例类,其中包含许多按钮。 The button click events are handled and monitored in seperate classes that have a reference/instance to the singleton winform class. 按钮单击事件是在单独的类中处理和监视的,这些类具有对singleton winform类的引用/实例。 So obviously when the user clicks the buttons then the tasks associated are handled in the relevant classes. 因此,很明显,当用户单击按钮时,将在相关类中处理关联的任务。

My question is, the classes that monitor the events are fully encapsulated, meaning that no methods are required to be called for them to work. 我的问题是,监视事件的类已完全封装,这意味着不需要调用任何方法即可使它们起作用。 The only thing they need is the event to be triggered and the tasks are carried out. 他们唯一需要的是要触发的事件和执行的任务。 Where should I therefore create the objects for these classes? 因此,应该在哪里为这些类创建对象? Should I simply create the objects within the winform when it is loaded? 加载时,是否应该在winform中简单地创建对象? No further interaction is required with the objects, they simply need an instance to monitor the button click events. 不需要与对象进行进一步的交互,它们只需要一个实例来监视按钮单击事件。 Hope i've explained this clearly enough. 希望我已经足够清楚地解释了这一点。

Thanks. 谢谢。

Yes, I think that the form's Load event is a logical place to create these objects. 是的,我认为表单的Load事件是创建这些对象的合理位置。 Or perhaps the constructor. 也许是构造函数。

You cannot create them before the form has been constructed, because there won't be any buttons then. 您不能在构建表单之前创建它们,因为那时将没有任何按钮。 And you shouldn't create it much later either, because then you might miss some events. 而且您也不应该在以后创建它,因为那样您可能会错过一些事件。 So the form's own events is the best place to create them. 因此,表单自身的事件是创建它们的最佳位置。 Or at least bind them to the events. 或者至少将它们绑定到事件。 Are these classes singletons as well? 这些课程也单身吗?

If you are making a real observer model, then the association belongs in the creator of the control. 如果要创建真实的观察者模型,则该关联属于控件的创建者。

If you are just pushing logic into separate classes, then it is more of a delegate pattern and it should be the control that owns the references. 如果只是将逻辑推入单独的类中,则它更多是一种委托模式,应该是拥有引用的控件。

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

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