简体   繁体   English

具有C#接口的继承窗口表单

[英]Inheritance windows forms with interfaces in C#

I have a base windows forms class which contains two buttons: Ok and Cancel. 我有一个基本的Windows窗体类,其中包含两个按钮:确定和取消。 This base form implements the interface IBaseForm : 此基本表单实现接口IBaseForm

public interface IBaseForm
{
  string FormText {get;set;}
  DialogResult ShowView(IWin32Window owner);
}

public partial class BaseForm : IBaseForm
{
  ...
}

Next I have a window class which inherits from BaseForm and implements another interface IItem : 接下来,我有一个继承自BaseForm并实现另一个接口IItem的窗口类:

public interface IItem : IBaseForm // here is problem
{
  string ItemName {get; set;}
  ...
}

public partial class AddItemForm : BaseForm, IItem
{
  ...
  // I don't have to implement IItem here, because it is implement in BaseForm
}

The problem is with the inheritance of IBaseForm ; 问题在于IBaseForm的继承。 I have to inherit it two times. 我必须继承两次。 In code I have: 在代码中,我有:

IItem view = new AddItemForm();
view.FormText = "Add new item";

If I remove inheritance from IItem , view.FormText will be not visible. 如果我从IItem删除继承,则view.FormText将不可见。 If I remove the inheritance from BaseForm I have to implement IBaseForm in each AddItemForm . 如果我从删除继承BaseForm我要实现IBaseForm每个AddItemForm I've only showed one itemForm but it is a lot, and I have to implement this many times. 我只显示了一个itemForm,但它很多,而且我必须执行多次。

How I can resolve this problem? 我该如何解决这个问题?

There isn't another way to do this. 没有其他方法可以做到这一点。 BaseForm is a class, whereas IItem is an unrelated interface. BaseForm是一个类,而IItem是一个不相关的接口。 Neither is a 'super' or 'base' of the other, so you need to inherit from IBaseForm in both of them. 两者都不是彼此的“超级”或“基础”,因此您都需要从IBaseForm继承它们。

I don't really understand the problem; 我不太了解这个问题; the code you've written compiles and runs just fine as you've written it. 您编写的代码可以编译并在您编写时正常运行。

AddItemForm inherits BaseForm 's implementation of IBaseForm , so doesn't have to re-implement IBaseForm . AddItemForm继承BaseForm的实施IBaseForm ,所以不必重新实现IBaseForm

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

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