private void OnInvokedDynamicItem(object sender, EventArgs args) { ThreadHelper.ThrowIfNotOnUIThread(); DynamicItemMenuCommand invokedCommand = (DynamicItemMenuCommand)sender; if (!invokedCommand.Enabled) { return; } int index = invokedCommand.MatchedCommandId == 0 ? 0 : invokedCommand.MatchedCommandId - DynamicItemMenuCommand.CommandId; if (index >= 0 && index < closedDocuments.Count) { int i = closedDocuments.Count - 1 - index; string fullName = closedDocuments[i].Original; closedDocuments.RemoveAt(i); try { //if (!dte.ItemOperations.IsFileOpen(fullName)) dte.ItemOperations.OpenFile(fullName); } catch (Exception) { // ignore } } }
/// <summary> /// Initializes a new instance of the <see cref="UndoCloseDocument"/> class. /// Adds our command handlers for menu (commands must exist in the command table file) /// </summary> /// <param name="package">Owner package, not null.</param> /// <param name="commandService">Command service to add command to, not null.</param> private UndoCloseDocument(UndoCloseDocumentPackage package, OleMenuCommandService commandService, IVsUIShell uiShell, DTE2 dte) { this.package = package ?? throw new ArgumentNullException(nameof(package)); commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); this.uiShell = uiShell ?? throw new ArgumentNullException("IVsUIShell"); this.dte = dte ?? throw new ArgumentNullException("DTE"); this.events = this.dte.Events ?? throw new NullReferenceException(nameof(this.events)); this.solutionEvents = this.events.SolutionEvents ?? throw new NullReferenceException("SolutionEvents"); this.solutionEvents.Opened += SolutionEvents_Opened; //this.solutionEvents.BeforeClosing += SolutionEvents_BeforeClosing; this.solutionEvents.AfterClosing += SolutionEvents_AfterClosing; //this.documentEvents = this.events.DocumentEvents ?? throw new NullReferenceException("DocumentEvents"); //this.documentEvents.DocumentOpening += DocumentEvents_DocumentOpening; //this.documentEvents.DocumentOpened += DocumentEvents_DocumentOpened; //this.documentEvents.DocumentClosing += DocumentEvents_DocumentClosing; this.windowEvents = this.events.WindowEvents ?? throw new NullReferenceException("WindowEvents"); this.windowEvents.WindowCreated += WindowEvents_WindowCreated; this.windowEvents.WindowClosing += WindowEvents_WindowClosing; //this.commandEvents = this.events.CommandEvents ?? throw new NullReferenceException("CommandEvents"); //this.commandEvents.BeforeExecute += CommandEvents_BeforeExecute; //this.commandEvents.AfterExecute += CommandEvents_AfterExecute; this.optionPage = package.GetDialogPage(typeof(UndoCloseDocumentOptionPage)) as UndoCloseDocumentOptionPage; if (this.optionPage == null) { throw new NullReferenceException("OptionPage"); } var menuCommandID = new CommandID(CommandSet, CommandId); //var menuItem = new MenuCommand(this.Execute, menuCommandID); var menuItem = new OleMenuCommand(this.Execute, null, OnBeforeQueryStatus, menuCommandID); commandService.AddCommand(menuItem); CommandID dynamicItemRootId = new CommandID(CommandSet, DynamicItemMenuCommand.CommandId); DynamicItemMenuCommand dynamicMenuCommand = new DynamicItemMenuCommand(dynamicItemRootId, IsValidDynamicItem, OnInvokedDynamicItem, OnBeforeQueryStatusDynamicItem); commandService.AddCommand(dynamicMenuCommand); }
private void OnBeforeQueryStatusDynamicItem(object sender, EventArgs args) { DynamicItemMenuCommand matchedCommand = (DynamicItemMenuCommand)sender; if (closedDocuments.Count > 0) { matchedCommand.Enabled = true; matchedCommand.Visible = true; int index = matchedCommand.MatchedCommandId == 0 ? 0 : matchedCommand.MatchedCommandId - DynamicItemMenuCommand.CommandId; if (index >= 0 && index < closedDocuments.Count) { int i = closedDocuments.Count - 1 - index; matchedCommand.Text = (index + 1).ToString() + " " + ShortifyPath(closedDocuments[i].Original, 60); } else { // just in case matchedCommand.Enabled = false; matchedCommand.Visible = false; matchedCommand.Text = "Error"; } } else { matchedCommand.Enabled = false; matchedCommand.Visible = true; matchedCommand.Text = "Empty"; } // Clear the ID because we are done with this item. matchedCommand.MatchedCommandId = 0; }
/// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place /// where you can put all the initilaization code that rely on services provided by VisualStudio. /// </summary> protected override void Initialize() { base.Initialize(); // Add our command handlers for menu (commands must exist in the .vsct file) OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; if ( null != mcs ) { // Create the command for the tool window CommandID toolwndCommandID = new CommandID(GuidList.guidDynamicStartItemExamplePackageCmdSet, PkgCmdIDList.cmdidShowDynamicItemCountEntryWindow); MenuCommand menuToolWin = new MenuCommand(ShowToolWindow, toolwndCommandID); mcs.AddCommand( menuToolWin ); //Add a 'dummy' entry for our menu, this entry has a null EventHandler as it will never be 'executed' //(you can't 'execute' a menu), and it doesn't set any properties as the default value of Visible is //true and we only want this handler in place so that the command system will find it once our package //is loaded and thus cause our main menu item to become visible. CommandID menuID = new CommandID(GuidList.guidDynamicStartItemExamplePackageCmdSet, PkgCmdIDList.mnuidMyMenu); MenuCommand menu = new MenuCommand(null, menuID); mcs.AddCommand(menu); //Add the DynamicItemMenuCommand that will be responsible for the expansion of our root item into N items //at runtime. Where N is the value the user has entered into our tool window. CommandID dynamicItemRootId = new CommandID(GuidList.guidDynamicStartItemExamplePackageCmdSet, PkgCmdIDList.cmdidMyDynamicStartItem); DynamicItemMenuCommand dynamicMenuCommand = new DynamicItemMenuCommand(dynamicItemRootId, IsValidDynamicItem, OnInvokedDynamicItem, OnBeforeQueryStatusDynamicItem); mcs.AddCommand(dynamicMenuCommand); } }