c# - 使用 WinForms 抽象表单

标签 c# winforms .net-3.5 compact-framework

我正在尝试在 WinForms 应用程序中实现与主窗体在 ASP.NET 中所做的类似的效果。我最初的想法是创建一个基本形式并将其声明为抽象形式,但编译器似乎不允许我这样做。

public abstract partial class Master : Form
{
    public Master()
    {
        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;
    }

编辑:

错误如下:

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 并没有真正的母版页的概念。我认为您最好的选择是使用 UserControl,并嵌套您的 UI 组合。

也许可以实现一个简单的接口(interface),比如让访问子控件更容易一些。

public interface IMasterPage
{
 IControlContainer Container { get; }
}

只是一些想法,祝你的项目好运。

关于c# - 使用 WinForms 抽象表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4692704/

相关文章:

c# - 如何将默认 Entity Framework 列添加到现有数据库

c# - 在 FlowLayoutPanel 中居中放置多行控件

c# - 如何在有限状态机实现中与用户通信?

asp.net - 如何使 .svc 文件写入 asp.net Trace.axd

c# - 访问静态资源中的属性

c# - .NET并发问题: Can I yield a semaphore to another thread

javascript - 从 Ajax 调用 .Net 中的 SOAP Web 方法时出现 500 服务器错误

xml - 将控件停靠在停靠的 FlowLayoutPanel 内

c# - 如何在组合框中显示枚举值?

c# - 参数化 DllImport 以在 C# 应用程序中使用