UIElement描述和實例
UIElement在SCSF中是隻視圖上的控件,包括MENU控件,TOOLBAR控件以及自定義控件等等,這裏重點是說一說自定義控件部分,因爲在Symphony中如果要統一視圖風格,自定義控件是必不可少的技術,而製作自定義控件在SCSF中需要先分別繼承並實現兩個接口:UIElementAdapter,IUIElementAdapterFactory。這樣才能在SCSF中進行加載和共享。
1. 利用SCSF創建一個Module並創建一個用戶控件,同時創建兩個類並分別繼承UIElementAdapter,IUIElementAdapterFactory兩個接口。
圖2.4.1
2. 在Adaptor和Factory兩個類中分別加入如下代碼:
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