Ejemplo n.º 1
0
        void InitializeMenuItems()
        {
            var dict = new Dictionary <Guid, Dictionary <string, MenuItemGroupMD> >();

            foreach (var item in mefMenuItems)
            {
                string ownerGuidString = item.Metadata.OwnerGuid ?? MenuConstants.CTX_MENU_GUID;
                bool   b = Guid.TryParse(ownerGuidString, out var ownerGuid);
                Debug.Assert(b, $"MenuItem: Couldn't parse OwnerGuid property: '{ownerGuidString}'");
                if (!b)
                {
                    continue;
                }

                string guidString = item.Metadata.Guid;
                if (guidString != null)
                {
                    b = Guid.TryParse(guidString, out var guid);
                    Debug.Assert(b, $"MenuItem: Couldn't parse Guid property: '{guidString}'");
                    if (!b)
                    {
                        continue;
                    }
                }

                b = !string.IsNullOrEmpty(item.Metadata.Group);
                Debug.Assert(b, "MenuItem: Group property is empty or null");
                if (!b)
                {
                    continue;
                }
                b = ParseGroup(item.Metadata.Group, out double groupOrder, out string groupName);
                Debug.Assert(b, "MenuItem: Group property must be of the format \"<order>,<name>\" where <order> is a System.Double");
                if (!b)
                {
                    continue;
                }

                if (!dict.TryGetValue(ownerGuid, out var groupDict))
                {
                    dict.Add(ownerGuid, groupDict = new Dictionary <string, MenuItemGroupMD>());
                }
                if (!groupDict.TryGetValue(groupName, out var mdGroup))
                {
                    groupDict.Add(groupName, mdGroup = new MenuItemGroupMD(groupOrder));
                }
                Debug.Assert(mdGroup.Order == groupOrder, $"MenuItem: Group order is different: {mdGroup.Order} vs {groupOrder}");
                mdGroup.Items.Add(new MenuItemMD(item));
            }

            guidToGroups = new Dictionary <Guid, List <MenuItemGroupMD> >();
            foreach (var kv in dict)
            {
                var groups = new List <MenuItemGroupMD>(kv.Value.Select(a => a.Value).OrderBy(a => a.Order));
                foreach (var g in groups)
                {
                    g.Items.Sort((a, b) => a.Metadata.Order.CompareTo(b.Metadata.Order));
                }
                guidToGroups.Add(kv.Key, groups);
            }
        }
Ejemplo n.º 2
0
        void InitializeMenuItems()
        {
            var dict = new Dictionary<Guid, Dictionary<string, MenuItemGroupMD>>();
            foreach (var item in mefMenuItems) {
                string ownerGuidString = item.Metadata.OwnerGuid ?? MenuConstants.CTX_MENU_GUID;
                Guid ownerGuid;
                bool b = Guid.TryParse(ownerGuidString, out ownerGuid);
                Debug.Assert(b, string.Format("MenuItem: Couldn't parse OwnerGuid property: '{0}'", ownerGuidString));
                if (!b)
                    continue;

                string guidString = item.Metadata.Guid;
                if (guidString != null) {
                    Guid guid;
                    b = Guid.TryParse(guidString, out guid);
                    Debug.Assert(b, string.Format("MenuItem: Couldn't parse Guid property: '{0}'", guidString));
                    if (!b)
                        continue;
                }

                b = !string.IsNullOrEmpty(item.Metadata.Group);
                Debug.Assert(b, "MenuItem: Group property is empty or null");
                if (!b)
                    continue;
                double groupOrder;
                string groupName;
                b = ParseGroup(item.Metadata.Group, out groupOrder, out groupName);
                Debug.Assert(b, "MenuItem: Group property must be of the format \"<order>,<name>\" where <order> is a System.Double");
                if (!b)
                    continue;

                Dictionary<string, MenuItemGroupMD> groupDict;
                if (!dict.TryGetValue(ownerGuid, out groupDict))
                    dict.Add(ownerGuid, groupDict = new Dictionary<string, MenuItemGroupMD>());
                MenuItemGroupMD mdGroup;
                if (!groupDict.TryGetValue(groupName, out mdGroup))
                    groupDict.Add(groupName, mdGroup = new MenuItemGroupMD(groupOrder));
                Debug.Assert(mdGroup.Order == groupOrder, string.Format("MenuItem: Group order is different: {0} vs {1}", mdGroup.Order, groupOrder));
                mdGroup.Items.Add(new MenuItemMD(item));
            }

            guidToGroups = new Dictionary<Guid, List<MenuItemGroupMD>>();
            foreach (var kv in dict) {
                var groups = new List<MenuItemGroupMD>(kv.Value.Select(a => a.Value).OrderBy(a => a.Order));
                foreach (var g in groups)
                    g.Items.Sort((a, b) => a.Metadata.Order.CompareTo(b.Metadata.Order));
                guidToGroups.Add(kv.Key, groups);
            }
        }