首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

.NET 組件編程(七):Component DesignerAction 智能標記實作

     編輯:關於.NET

DesignerActionList 與 DesignerAction 讓組件/控件在智能標記面板顯示情境相關資訊與命令,可視為設計器動詞的替代方案。DesignerActionList 為主要互動點,開發者繼承並覆寫 GetSortedActionItems() 回傳 DesignerActionItemCollection。

四種 DesignerActionItem 衍生類別:

  • DesignerActionMethodItem:方法項,點擊執行方法
  • DesignerActionPropertyItem:屬性項,直接在面板設定屬性
  • DesignerActionTextItem:文字項
  • DesignerActionHeaderItem:標題項,具分組功能

完整範例

[Designer(typeof(CustomerDesigner), typeof(IDesigner))]
public class Customer : Component {
    public string Id { get; set; }
    public Sex Sex { get; set; }
    public string Address { get; set; }
}
public enum Sex { 男 = 0, 女 = 1 }

public class CustomerDesigner : ComponentDesigner {
    private DesignerActionListCollection actionLists;
    public override DesignerActionListCollection ActionLists {
        get {
            if (actionLists == null) {
                actionLists = new DesignerActionListCollection();
                actionLists.Add(new CustomerActionList(this.Component));
            }
            return actionLists;
        }
    }
    // 右鍵選單項(若覆寫 ActionLists 則不出現在智能標記)
    public CustomerDesigner() {
        this.Verbs.Add(new DesignerVerb("Click2", OnClick2));
    }
    void OnClick2(object s, EventArgs e) { MessageBox.Show("Click2."); }
}

public class CustomerActionList : DesignerActionList {
    private Customer customer;
    public CustomerActionList(IComponent component) : base(component) {
        customer = component as Customer;
    }
    // 透過 TypeDescriptor 讀寫屬性,確保設計時序列化
    public string Id {
        get { return customer.Id; }
        set { TypeDescriptor.GetProperties(typeof(Customer))["Id"].SetValue(customer, value); }
    }
    public Sex Sex {
        get { return customer.Sex; }
        set { TypeDescriptor.GetProperties(typeof(Customer))["Sex"].SetValue(customer, value); }
    }
    public void OnClick1() { MessageBox.Show("Click1."); }
    public override DesignerActionItemCollection GetSortedActionItems() {
        var items = new DesignerActionItemCollection();
        items.Add(new DesignerActionHeaderItem("Customer's Info", "Info"));
        items.Add(new DesignerActionHeaderItem("Customer's Action", "Action"));
        items.Add(new DesignerActionHeaderItem("Description"));
        items.Add(new DesignerActionPropertyItem("Id", "Id", "Info", "Customer's id"));
        items.Add(new DesignerActionPropertyItem("Sex", "Sex", "Info", "Customer's sex"));
        items.Add(new DesignerActionMethodItem(this, "OnClick1", "Click1", "Action", true));
        items.Add(new DesignerActionTextItem("https://mapserver.cnblogs.com", "Description"));
        return items;
    }
}

效果:智能標記面板顯示分組、屬性編輯、方法按鈕與連結文字。
智能標記面板 右鍵選單

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved