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

}

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

最佳答案

我有一个示例,你可以看看它就是这样做的。希望这会对你有所帮助。如果您看完后有任何具体问题,请告诉我!
http://dl.getdropbox.com/u/376992/CAGMenus.zip

希望这可以帮助。

关于service - Prism 应用程序 CAL 中的菜单服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1516164/

10-10 20:19