Prism (CAL) フレームワークを作成しようとしています (これが CAL への最初の進出です)。地域のメニュー項目を登録するためのサービスを考案するのに苦労しています)。これが私がこれまでに思いついたものです...
/// <summary>
/// Menu item service - allows any module to register menu items with any region
/// </summary>
public class MenuService : IMenuService
{
private IUnityContainer m_UnityContainer;
private IRegionManager m_RegionManager;
private Dictionary<string, IUnityContainer> m_MenuContainers = new Dictionary<string,IUnityContainer>();
/// <summary>
/// Injection ctor
/// </summary>
/// <param name="unityContainer"></param>
/// <param name="regionManager"></param>
public MenuService(IUnityContainer unityContainer, IRegionManager regionManager)
{
m_UnityContainer = unityContainer;
m_RegionManager = regionManager;
}
/// <summary>
/// Registers a menu item with the service
/// </summary>
/// <param name="topLevelName"></param>
/// <param name="regionIdentity"></param>
/// <param name="displayText"></param>
public void RegisterMenuItem(string topLevelName, string regionIdentity, string displayText)
{
IUnityContainer container = GetOrCreateUiIdentityContainer(regionIdentity);
if (string.IsNullOrEmpty(topLevelName))
container.RegisterInstance("MENUITEM" + displayText, new MenuItem { Header = displayText} );
else
{
MenuItem topLevel = container.Resolve<MenuItem>("MENUITEM" + topLevelName);
if (topLevel.Header == null)
{
topLevel = new MenuItem{ Header = topLevelName};
container.RegisterInstance<MenuItem>("MENUITEM" + topLevelName, topLevel);
}
MenuItem newItem = new MenuItem{Header = displayText};
topLevel.Items.Add(newItem);
container.RegisterInstance("MENUITEM" + topLevelName + "_" + displayText, newItem);
}
}
private IUnityContainer GetOrCreateUiIdentityContainer(string regionIdentity)
{
IUnityContainer container;
container = GetUnityContainer(regionIdentity);
if (container != null)
return container;
container = m_UnityContainer.CreateChildContainer();
m_MenuContainers.Add(regionIdentity, container);
return container;
}
private IUnityContainer GetUnityContainer(string regionIdentity)
{
IUnityContainer container;
m_MenuContainers.TryGetValue(regionIdentity, out container);
return container;
}
/// <summary>
/// Builds up the menu for a given region
/// </summary>
/// <param name="regionIdentity"></param>
public void BuildMenu(string regionIdentity)
{
IUnityContainer container;
container = GetUnityContainer(regionIdentity);
if (container == null)
return;
IRegion region = m_RegionManager.Regions[regionIdentity];
if (region == null)
return;
IEnumerable<MenuItem> menuItems = container.ResolveAll<MenuItem>();
foreach (MenuItem item in menuItems)
{
if (item.Role == MenuItemRole.SubmenuHeader )
region.Add(item);
}
}
}
私がやろうとしていることを行うためのより良い方法はありますか? 乾杯、ジェイソン