Menu service in Prism application CAL

。_饼干妹妹 提交于 2019-11-30 18:45:38

问题


I am trying to create a Prism (CAL) framework (this is my first forray into CAL) and am having difficulty devising a service for registering menu items for a region). Here is what I have come up with so far...

/// <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);
        }
    }

}

Is there a better way to do what I am trying to do? Cheers, Jason


回答1:


I've got a sample you can take a look at that does exactly this. Hopefully this will help you out a bit. If you have any specific questions after looking at it, let me know! http://dl.getdropbox.com/u/376992/CAGMenus.zip

Hope this helps.



来源:https://stackoverflow.com/questions/1516164/menu-service-in-prism-application-cal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!