用C#中的接口继承windows窗体

发布于 2024-12-11 12:32:53 字数 960 浏览 0 评论 0原文

我有一个基本的 Windows 窗体类,其中包含两个按钮:“确定”和“取消”。这个基本表单实现了接口IBaseForm

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

public partial class BaseForm : IBaseForm
{
  ...
}

接下来我有一个窗口类,它继承自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
}

问题在于IBaseForm 的继承;我必须继承它两次。 在我的代码中:

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

如果我从 IItem 中删除继承,view.FormText 将不可见。如果我从 BaseForm 中删除继承,我必须在每个 AddItemForm 中实现 IBaseForm。我只展示了一个 itemForm,但它很多,而且我必须多次实现。

我该如何解决这个问题?

I have a base windows forms class which contains two buttons: Ok and Cancel. This base form implements the interface 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:

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; 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. If I remove the inheritance from BaseForm I have to implement IBaseForm in each AddItemForm. I've only showed one itemForm but it is a lot, and I have to implement this many times.

How I can resolve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

好多鱼好多余 2024-12-18 12:32:53

没有其他方法可以做到这一点。 BaseForm 是一个类,而 IItem 是一个不相关的接口。两者都不是另一个的“超级”或“基础”,因此您需要从它们中的 IBaseForm 继承。

There isn't another way to do this. BaseForm is a class, whereas IItem is an unrelated interface. Neither is a 'super' or 'base' of the other, so you need to inherit from IBaseForm in both of them.

不忘初心 2024-12-18 12:32:53

我不太明白这个问题;您编写的代码可以像您编写的那样编译并运行得很好。

AddItemForm继承了BaseFormIBaseForm的实现,因此不必重新实现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.

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