在棱镜应用校准菜单服务(Menu service in Prism application CAL)

2019-07-20 19:10发布

我想创建一个棱镜(CAL)框架(这是我第一次到forray 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);
        }
    }

}

有没有更好的方式做什么,我试图做的? 干杯,杰森

Answer 1:

我有在那不正是这一点,你可以看看样品。 希望这会帮助你有点。 如果你看它之后的任何具体问题,让我知道! http://dl.getdropbox.com/u/376992/CAGMenus.zip

希望这可以帮助。



文章来源: Menu service in Prism application CAL