SmartClient Software factory中的Composite UI Application Block(Cab)技術瞭解(三):UIElement

UIElement描述和實例

       UIElementSCSF中是隻視圖上的控件,包括MENU控件,TOOLBAR控件以及自定義控件等等,這裏重點是說一說自定義控件部分,因爲在Symphony中如果要統一視圖風格,自定義控件是必不可少的技術,而製作自定義控件在SCSF中需要先分別繼承並實現兩個接口:UIElementAdapterIUIElementAdapterFactory。這樣才能在SCSF中進行加載和共享。

1.         利用SCSF創建一個Module並創建一個用戶控件,同時創建兩個類並分別繼承UIElementAdapterIUIElementAdapterFactory兩個接口。

2.4.1

2.         AdaptorFactory兩個類中分別加入如下代碼:

    public class CustomerUIElementAdaptor : UIElementAdapter<String>

    {

        CustomerUIElement _uiObj;

        public CustomerUIElementAdaptor(CustomerUIElement uiobj)

        {

            this._uiObj = uiobj;

        }

        protected override string Add(string uiElement)

        {

            this._uiObj.Text = uiElement;

            return uiElement;

        }

 

        protected override void Remove(string uiElement)

        {

            string ss = this._uiObj.Text;

            if (ss.Contains(uiElement))

            {

                ss.Replace(uiElement, "");

            }

        }

}

    public class CustomerUIElementFactory : IUIElementAdapterFactory

    {

        #region IUIElementAdapterFactory 成員

 

        public IUIElementAdapter GetAdapter(object uiElement)

        {

            if (uiElement is CustomerUIElement)

            {

                return new CustomerUIElementAdaptor(uiElement as CustomerUIElement);

            }

            throw new ArgumentException("uiElement");

           

        }

 

        public bool Supports(object uiElement)

        {

            return uiElement is CustomerUIElement;

        }

 

        #endregion

    }

剩下把控件視圖設計好。這樣一個自定義控件就已經完成,接下來就是如何在其他視圖中使用該自定義控件了。

3.         Module中的一個VIEW中添加該自定義控件:

2.4.2

增加一個類ModuleActions,並添加相關代碼:

public class ModuleActions

    {

        private WorkItem _workItem = null;

 

        [ServiceDependency]

        public WorkItem WorkItem

        {

            get { return _workItem; }

            set { _workItem = value; }

        }

 

        [Action(ActionNames.ShowCustomerUIElement)]

        public void showCustomerUiElement(object caller, object target)

        {

            UIExtensionSite site = WorkItem.UIExtensionSites[UIExtensionSiteNames.CustomerUiElement];

            String ss = "test the customer uielement block";

            site.Add(ss);

        }

        [Action(ActionNames.ViewCustomerUIElement)]

        public void addCustomerUiElement(object caller, object target)

        {

            UiElementView view = WorkItem.Items.AddNew<UiElementView>();

            WorkItem.Workspaces[WorkspaceNames.LayoutUIElementView].Show(view);

            WorkItem.UIExtensionSites.RegisterSite(UIExtensionSiteNames.CustomerUiElement, view.customerView);

        

        }

    }

在類ModuleController中添加代碼:

            WorkItem.Items.AddNew<ModuleActions>();

            ActionCatalogService.Execute(ActionNames.ViewCustomerUIElement, WorkItem, this, null);

            ActionCatalogService.Execute(ActionNames.ShowCustomerUIElement, WorkItem, this, null);

在類SmartClientApplication中增加相關服務代碼:

            RootWorkItem.Services.AddOnDemand<ActionCatalogService, IActionCatalogService>();

            IUIElementAdapterFactoryCatalog catalog = RootWorkItem.Services.Get<IUIElementAdapterFactoryCatalog>();

            catalog.RegisterFactory(new CustomerUIElementFactory());

在配置文件中配置好後直接運行,就可得到如圖2.4.3結果。

2.4.3

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章