views:

488

answers:

1

Hello all, 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

A: 

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.

Anderson Imes