每個Component不但是有Component Designer,而且還有Component DocumentDesigner,但這兩個 Designer之間到底有什麼樣的區別呢?我用比較通俗的講法來給大家區別下,Component Designer是指一 個Component被拖放到Form或者Page的Container上時所呈現出來的UI設計器(圖1);Component DocumentDesigner則是指Component本身根文檔設計器(圖2)。
比如有兩個Component,其名稱為BaseClass1和Class1,且Class1繼承於BaseClass1,我們為 BaseClass1設計了DocumentDesigner,設計項在BaseClass1的文檔設計器中不可見,在Class1中則可見。
代碼如下:
BaseClass1
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
namespace ClassLibrary1
{
// 請注意typeof(IRootDesigner)。
[Designer(typeof(BaseClass1DocumentDesigner), typeof(IRootDesigner))]
public class BaseClass1 : Component
{
}
// 定義DocumentDesigner。
public class BaseClass1DocumentDesigner : ComponentDocumentDesigner
{
public BaseClass1DocumentDesigner()
{
DesignerVerb verb1 = new DesignerVerb("Test1", new EventHandler (OnTest1));
this.Verbs.Add(verb1);
}
private void OnTest1(object sender, EventArgs e)
{
MessageBox.Show("Test1");
}
}
}
Class1
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.ComponentModel.Design;
using System.Windows.Forms;
namespace ClassLibrary1
{
[Designer(typeof(Class1Designer), typeof(IDesigner))]
public partial class Class1 : BaseClass1
{
}
public class Class1Designer : ComponentDesigner
{
public Class1Designer()
{
DesignerVerb verb2 = new DesignerVerb("Test2", new EventHandler (OnTest2));
this.Verbs.Add(verb2);
}
private void OnTest2(object sender, EventArgs e)
{
MessageBox.Show("Test2");
}
}
}
效果如下: