private int CreateMenuItem() { ValidateData(); var mItem = new AdvMenuItem { MenuItemName = txtName.Text, MenuItemParentID = string.IsNullOrEmpty(hParent.Value) ? 0 : Convert.ToInt32(hParent.Value), MenuItemUrlPath = txtUrl.Text, Enabled = ckbEnabled.Checked, Blank = ckbBlank.Checked, SortOrder = Convert.ToInt32(txtSortOrder.Text), MenuItemUrlType = (EMenuItemUrlType)Convert.ToInt32(rblLinkType.SelectedValue), ShowMode = (EMenuItemShowMode)Convert.ToInt32(ddlShowMode.SelectedValue) }; if (_type == MenuService.EMenuType.Top) { CacheManager.RemoveByPattern(CacheNames.GetMainMenuCacheObjectName()); CacheManager.RemoveByPattern(CacheNames.GetMainMenuAuthCacheObjectName()); } else if (_type == MenuService.EMenuType.Bottom) { var cacheName = CacheNames.GetBottomMenuCacheObjectName(); if (CacheManager.Contains(cacheName)) { CacheManager.Remove(cacheName); } var cacheAuthName = CacheNames.GetBottomMenuAuthCacheObjectName(); if (CacheManager.Contains(cacheAuthName)) { CacheManager.Remove(cacheAuthName); } } mItem.MenuItemID = MenuService.AddMenuItem(mItem, _type); _menuItemId = mItem.MenuItemID; if (IconFileUpload.HasFile) { using (IconFileUpload.FileContent) { var tempName = PhotoService.AddPhoto(new Photo(0, _menuItemId, PhotoType.MenuIcon) { OriginName = IconFileUpload.FileName }); if (!string.IsNullOrWhiteSpace(tempName)) { IconFileUpload.SaveAs(FoldersHelper.GetPathAbsolut(FolderType.MenuIcons, tempName)); } } } else { mItem.MenuItemIcon = string.Empty; } MenuService.UpdateMenuItem(mItem, _type); return(mItem.MenuItemID); }
/// <summary> /// 添加插件到菜单项 /// </summary> /// <param name="tsbParent"></param> /// <param name="plugin"></param> /// <param name="menuService"></param> public static void AddPluginToMenu(ToolStripDropDownButton tsbParent, IPlugin plugin, MenuService menuService) { if (plugin != null) { ToolStripMenuItem mi = new ToolStripMenuItem(); ToolStripMenuItem miChild = null; mi.Text = plugin.PluginInfo.Name; mi.Tag = plugin.PluginInfo.GUID; mi.Click += RunPlugin; if (plugin.ChildNodes.Count > 0) { foreach (System.Collections.Generic.KeyValuePair <string, EventHandler> menuItem in plugin.ChildNodes) { if (menuItem.Key == "%Split%") { ToolStripSeparator miSplit = new ToolStripSeparator(); mi.DropDownItems.Add(miSplit); } else { miChild = new ToolStripMenuItem(); miChild.Text = menuItem.Key; miChild.Click += menuItem.Value; mi.DropDownItems.Add(miChild); } } } tsbParent.DropDownItems.Add(mi); menuService.AddMenuItem(plugin.PluginInfo.GUID, mi); } }
public void CanAddOnSpecyficPathNewMenuItem() { IMenuService service = new MenuService(commandsViewModel.Object); commandsViewModel.SetupProperty(x => x.Commands) .SetupGet(x => x.Commands) .Returns(new ObservableCollection <MenuItem>()); var newItem = new MenuItem { Header = "Asd" }; service.AddMenuItem(newItem, "#MenuItems:"); Assert.IsNotNull(commandsViewModel.Object.Commands); Assert.AreEqual(commandsViewModel.Object.Commands.Count, 1); Assert.AreEqual(commandsViewModel.Object.Commands.First().Header, "Asd"); var otherSubItem = new MenuItem { Header = "SubItem" }; service.AddMenuItem(otherSubItem, "#MenuItems:Asd"); Assert.IsNotNull(commandsViewModel.Object.Commands); Assert.AreEqual(commandsViewModel.Object.Commands.Count, 1); Assert.AreEqual(commandsViewModel.Object.Commands.First().Header, "Asd"); Assert.IsNotNull(commandsViewModel.Object.Commands.SingleOrDefault(x => x.Header.Equals("Asd"))); Assert.AreEqual(commandsViewModel.Object.Commands.SingleOrDefault(x => x.Header.Equals("Asd")).Items.Count, 1); Assert.AreEqual( ((MenuItem)(commandsViewModel.Object.Commands.SingleOrDefault(x => x.Header.Equals("Asd")).Items[0])) .Header, "SubItem"); var otherSubSubItem = new MenuItem { Header = "SubSubItem" }; service.AddMenuItem(otherSubSubItem, "#MenuItems:Asd|SubItem"); Assert.IsNotNull(commandsViewModel.Object.Commands); Assert.AreEqual(commandsViewModel.Object.Commands.Count, 1); Assert.AreEqual(commandsViewModel.Object.Commands.First().Header, "Asd"); Assert.IsNotNull(commandsViewModel.Object.Commands.SingleOrDefault(x => x.Header.Equals("Asd"))); Assert.AreEqual(commandsViewModel.Object.Commands.SingleOrDefault(x => x.Header.Equals("Asd")).Items.Count, 1); Assert.AreEqual( ((MenuItem)(commandsViewModel.Object.Commands.SingleOrDefault(x => x.Header.Equals("Asd")).Items[0])) .Header, "SubItem"); Assert.AreEqual( ((MenuItem) ((MenuItem) (commandsViewModel.Object.Commands.SingleOrDefault(x => x.Header.Equals("Asd")).Items[0])).Items [0]).Header, "SubSubItem"); Assert.AreEqual( ((MenuItem) ((MenuItem) (commandsViewModel.Object.Commands.SingleOrDefault(x => x.Header.Equals("Asd")).Items[0])).Items [0]).Uid, "#MenuItems:Asd|SubItem|SubSubItem"); }