C#将接口应用于表单类

时间:2022-01-07 05:40:53

How can I apply an Interface to a form class

如何将接口应用于表单类

partial class Form1 : Form, InterfaceA

Is this correct?

它是否正确?

Basically I would like to implement an Interface on a form. How To ....

基本上我想在表单上实现一个接口。如何 ....

3 个解决方案

#1


6  

A Form is just a class (that subclasses System.Windows.Forms.Form), so yes - standard syntax is fine, as you have it.

Form只是一个类(它是System.Windows.Forms.Form的子类),所以是 - 标准语法很好,就像你拥有它一样。

Edit: As to your partial class part of the question, no, you need only declare that you implement the interface once. From MSDN...

编辑:关于问题的部分类部分,不,您只需声明您实现了一次接口。来自MSDN ......

If any of the parts are declared abstract, then the entire type is considered abstract. If any of the parts are declared sealed, then the entire type is considered sealed. If any of the parts declare a base type, then the entire type inherits that class.

如果任何部分被声明为abstract,那么整个类型被认为是抽象的。如果任何部件被宣布密封,则整个类型被认为是密封的。如果任何部分声明了基类型,则整个类型继承该类。

Remember, there's no magic in forms or partial classes. C#/.Net is one of the few Microsoft projects that's wizardry free - it really does tend to behave the way you think it should.

请记住,形式或部分类没有魔力。 C#/ .Net是为数不多的免费魔法项目之一 - 它确实倾向于以你认为应该的方式行事。

#2


1  

Yes - a form is just a class at the end of the day

是的 - 表格在一天结束时只是一个班级

#3


0  

When working with partial classes in C#, either:

在C#中使用部分类时,要么:

  • any declaration with the ':' operator must specify exactly the same baseclass and interfaces
  • 使用':'运算符的任何声明都必须指定完全相同的基类和接口

  • specifying baseclass and interfaces on one of the declarations will suffice
  • 在其中一个声明上指定基类和接口就足够了

To make life easier for yourself, add the interface specs in only one place (without checking I suspect this is in the designer class part by default when working with the WinForms designer).

为了让自己的生活更轻松,只需在一个地方添加界面规范(在没有检查的情况下,我怀疑在使用WinForms设计器时默认情况下这是在设计器类部件中)。

#1


6  

A Form is just a class (that subclasses System.Windows.Forms.Form), so yes - standard syntax is fine, as you have it.

Form只是一个类(它是System.Windows.Forms.Form的子类),所以是 - 标准语法很好,就像你拥有它一样。

Edit: As to your partial class part of the question, no, you need only declare that you implement the interface once. From MSDN...

编辑:关于问题的部分类部分,不,您只需声明您实现了一次接口。来自MSDN ......

If any of the parts are declared abstract, then the entire type is considered abstract. If any of the parts are declared sealed, then the entire type is considered sealed. If any of the parts declare a base type, then the entire type inherits that class.

如果任何部分被声明为abstract,那么整个类型被认为是抽象的。如果任何部件被宣布密封,则整个类型被认为是密封的。如果任何部分声明了基类型,则整个类型继承该类。

Remember, there's no magic in forms or partial classes. C#/.Net is one of the few Microsoft projects that's wizardry free - it really does tend to behave the way you think it should.

请记住,形式或部分类没有魔力。 C#/ .Net是为数不多的免费魔法项目之一 - 它确实倾向于以你认为应该的方式行事。

#2


1  

Yes - a form is just a class at the end of the day

是的 - 表格在一天结束时只是一个班级

#3


0  

When working with partial classes in C#, either:

在C#中使用部分类时,要么:

  • any declaration with the ':' operator must specify exactly the same baseclass and interfaces
  • 使用':'运算符的任何声明都必须指定完全相同的基类和接口

  • specifying baseclass and interfaces on one of the declarations will suffice
  • 在其中一个声明上指定基类和接口就足够了

To make life easier for yourself, add the interface specs in only one place (without checking I suspect this is in the designer class part by default when working with the WinForms designer).

为了让自己的生活更轻松,只需在一个地方添加界面规范(在没有检查的情况下,我怀疑在使用WinForms设计器时默认情况下这是在设计器类部件中)。