重要的类:
1.UIElementAdapterFactoryCatalog 负责管理UI元素适配器工厂,主要方法:
1.1RegisterFactory 注册UIElementAdapterFactory
1.2GetFactory 根据UI元素获取相应的适配器工厂,其实现如下:
public IUIElementAdapterFactory GetFactory(object element)
{
foreach (IUIElementAdapterFactory factory in factories)
{
if (factory.Supports(element))
return factory;
}
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
Properties.Resources.NoRegisteredUIElementFactory,
element.GetType().ToString()));
}
2.IUIElementAdapterFactory 负责创建UI元素适配器,主要方法:
2.1 IUIElementAdapter GetAdapter(object uiElement) 根据UI元素创建相应的适配器
2.2 bool Supports(object uiElement) 判断该工厂是否可以创建该元素的适配器
3.IUIElementAdapter 负责增删元素
3.1 object Add(object uiElement)
3.2 void Remove(object uiElement);
4.UIExtensionSiteCollection UI扩展站点集合,负责管理UIExtensionSite
4.1 RegisterSite(string siteName, object uiElement) 注册UI站点
public void RegisterSite(string siteName, object uiElement)
{
Guard.ArgumentNotNullOrEmptyString(siteName, "siteName");
Guard.ArgumentNotNull(uiElement, "uiElement");
IUIElementAdapterFactory factory = FactoryCatalog.GetFactory(uiElement);
IUIElementAdapter adapter = factory.GetAdapter(uiElement);
createdAdapters.Add(adapter);
RegisterSite(siteName, adapter);
}
public void RegisterSite(string siteName, IUIElementAdapter adapter)
{
Guard.ArgumentNotNullOrEmptyString(siteName, "siteName");
Guard.ArgumentNotNull(adapter, "adapter");
if (Contains(siteName))
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
Properties.Resources.DuplicateUIExtensionSite, siteName), "siteName");
sites.Add(siteName, new UIExtensionSite(adapter));
}
4.2 UIExtensionSite this[string siteName] 根据名称返回UI站点
5.UIExtensionSite 负责利用IUIElementAdapter 增删元素
使用UI元素扩展流程:
1.注册适配器工厂:
rootWorkItem.Services.AddNew<UIElementAdapterFactoryCatalog, IUIElementAdapterFactoryCatalog>();
2.注册UI扩展站点:
RootWorkItem.UIExtensionSites.RegisterSite(UIExtensionConstants.MAINMENU, Shell.MainMenuStrip)
3.在站点上添加元素:
UIExtensionSites[UIExtensionConstants.FILE].Add(queueItem);