2011-01-14 15 views
3

我試圖在WinForms應用程序中實現與ASP.NET主窗體一樣的類似效果。我最初的想法是創建一個基本表單並將其聲明爲抽象,但編譯器似乎不讓我這樣做。使用WinForms的抽象窗體

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

所以我有兩個問題:

  • 爲什麼編譯器不會讓我這樣做呢?我是否使用了錯誤的語法,或者這是一個真正的限制。
  • 任何人都可以提出一種解決方法或更好的方法來做到這一點?

編輯:

的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. 
+1

實際的編譯器錯誤是什麼? – 2011-01-14 15:33:09

+0

如果這是你試圖編譯的確切代碼片段,你需要爲InitializeComponent()提供一個定義 – asawyer 2011-01-14 15:35:28

回答

0

我試圖實現類似的af在WinForms應用程序中以 形式與ASP.NET一起使用。

Winforms實際上並沒有母版頁的概念。我想你最好的選擇是使用UserControls,並嵌套你的UI組成。

也許實現一個簡單的界面,就像使訪問子控件更容易一些。

public interface IMasterPage 
{ 
IControlContainer Container { get; } 
} 

只是有些想法,祝你的項目好運。