Prism应用程序CAL中的菜单服务

时间:2009-10-04 11:06:37

标签: service menu prism decoupling cal

我正在尝试创建一个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);
        }
    }

}

有没有更好的方法来做我想做的事情? 干杯, 杰森

1 个答案:

答案 0 :(得分:1)

我有一个样本,你可以看看这是否正是如此。希望这会对你有所帮助。如果您在查看后有任何具体问题,请告诉我们! http://dl.getdropbox.com/u/376992/CAGMenus.zip

希望这有帮助。