问题
I was trying to add a second tool window to a VSPackage project in Visual Studio, I have a project with a tool window already created usin the wizard provided by Visual Studio when a VSPackage project is created, I was surfing the web looking for some tutorial that can help me adding a second tool window to my existing VSPackage project. I have read several articles about tool windows but I can't get with a solution. I create a new class
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
namespace Company.VSPackage1
{
[Guid("759c7eb3-6850-4cce-b765-2d5902a90918")]
public class OtherToolWindow : ToolWindowPane
{
public OtherToolWindow() :
base(null)
{
this.Caption = Resources.OtherToolWindowTitle;
this.BitmapResourceID = 301;
this.BitmapIndex = 1;
}
}
}
And then I modify the class that inherited from Package several times but something I'm doing wrong or missing
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
namespace Company.VSPackage1
{
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideToolWindow(typeof(MyToolWindow))]
[ProvideToolWindow(typeof(OtherToolWindow))]
[Guid(GuidList.guidVSPackage1PkgString)]
public sealed class VSPackage1Package : Package
{
public VSPackage1Package()
{
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
}
private void ShowToolWindow(object sender, EventArgs e)
{
ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true);
if ((null == window) || (null == window.Frame))
{
throw new NotSupportedException(Resources.CanNotCreateWindow);
}
IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
}
private void ShowOtherToolWindow(object sender, EventArgs e)
{
ToolWindowPane otherWindow = this.FindToolWindow(typeof(OtherToolWindow), 0, true);
if ((null == otherWindow) || (null == otherWindow.Frame))
{
throw new NotSupportedException(Resources.CanNotCreateWindow);
}
IVsWindowFrame otherWindowFrame = (IVsWindowFrame)otherWindow.Frame;
Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(otherWindowFrame.Show());
}
protected override void Initialize()
{
Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
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.guidVSPackage1CmdSet, (int)PkgCmdIDList.cmdidMyTool);
MenuCommand menuToolWin = new MenuCommand(ShowToolWindow, toolwndCommandID);
CommandID toolwndCommandID2 = new CommandID(GuidList.guidVSPackage1CmdSet2, (int)PkgCmdIDList.cmdidMyTool2);
MenuCommand menuToolWin2 = new MenuCommand(ShowOtherToolWindow, toolwndCommandID2);
mcs.AddCommand( menuToolWin );
mcs.AddCommand(menuToolWin2);
}
}
}
}
I just want to add more than one tool window in the same vspackage in visual studio
回答1:
This is wrong for sure:
CommandID toolwndCommandID2 = new CommandID(GuidList.guidVSPackage1CmdSet2, (int)PkgCmdIDList.cmdidMyTool2);
It should be:
CommandID toolwndCommandID2 = new CommandID(GuidList.guidVSPackage1CmdSet, (int)PkgCmdIDList.cmdidMyTool2);
And you need to fix the .vsct file and Guids.cs file, which you haven't posted.
That is, a package has a single command set, which can have several commands.
FWIW, I am working on a tutorial about creating toolwindows. Here it is:
HOWTO: Create a toolwindow with a ToolWindowPane class in a Visual Studio package http://www.visualstudioextensibility.com/2015/02/20/mz-tools-articles-series-howto-create-a-toolwindow-with-a-toolwindowpane-class-in-a-visual-studio-package/
来源:https://stackoverflow.com/questions/28620932/add-two-or-more-tool-window-into-the-same-visual-studio-project