简体   繁体   English

使用WinForms的抽象表单

[英]Abstract Forms using WinForms

I'm trying to achieve a similar affect in a WinForms application as a master form does with ASP.NET. 我试图在WinForms应用程序中实现类似的效果,因为主窗体与ASP.NET一样。 My initial thoughts on this were to create a base form and declare it as abstract, but the compiler doesn't seem to let me do this. 我最初的想法是创建一个基本表单并将其声明为抽象,但编译器似乎不允许我这样做。

public abstract partial class Master : Form
{
    public Master()
    {
        InitializeComponent();
    }
}

So I have two questions: 所以我有两个问题:

  • Why will the compiler not let me do this? 为什么编译器不允许我这样做? Am I using the wrong syntax or is this a genuine restriction. 我使用错误的语法还是这是一个真正的限制。
  • Can anyone suggest a workaround or better way to do this? 任何人都可以建议一种解决方法或更好的方法吗?

EDIT: 编辑:

InitializeComponent code: InitializeComponent代码:

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.Menu = this.mainMenu1;
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
        this.Text = "Master";
        this.AutoScroll = true;
    }

EDIT: 编辑:

The error is as follows: 错误如下:

The designer must create an instance of type 'Namespace.Master' but it cannot because the type is declared as abstract.

I'm trying to achieve a similar affect in a WinForms application as a master form does with ASP.NET. 我试图在WinForms应用程序中实现类似的效果,因为主窗体与ASP.NET一样。

Winforms doesn't really have the concept of a master page. Winforms并不真正具有母版页的概念。 I imagine your best bet is to use UserControls, and nest your UI composition. 我想你最好的选择是使用UserControls,并嵌套你的UI组合。

Maybe implement a simple interface like something like to make accessing the sub controls a little easier. 也许实现一个简单的界面,就像让访问子控件更容易一些。

public interface IMasterPage
{
 IControlContainer Container { get; }
}

Just some thoughts, good luck with your project. 只是一些想法,祝你的项目好运。

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

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