C# 将两个或多个工具窗口添加到同一VisualStudio项目中

C# 将两个或多个工具窗口添加到同一VisualStudio项目中,c#,visual-studio-2013,visual-studio-extensions,vspackage,C#,Visual Studio 2013,Visual Studio Extensions,Vspackage,我试图在Visual Studio中向VSPackage项目添加第二个工具窗口,我有一个已经使用Visual Studio提供的向导创建了工具窗口的项目创建VSPackage项目时,我正在网上冲浪,寻找一些教程,可以帮助我向现有VSPackage项目添加第二个工具窗口。我读过几篇关于工具窗口的文章,但我找不到解决方案。我创建了一个新类 using System; using System.Collections; using System.ComponentModel; using System

我试图在Visual Studio中向VSPackage项目添加第二个工具窗口,我有一个已经使用Visual Studio提供的向导创建了工具窗口的项目创建VSPackage项目时,我正在网上冲浪,寻找一些教程,可以帮助我向现有VSPackage项目添加第二个工具窗口。我读过几篇关于工具窗口的文章,但我找不到解决方案。我创建了一个新类

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;
        }
    }
}
然后我多次修改从包中继承的类,但有些地方我做得不对或缺少

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);
            }

        }
    }
}

我只想在visual studio中的同一vspackage中添加多个工具窗口这肯定是错误的:

 CommandID toolwndCommandID2 = new CommandID(GuidList.guidVSPackage1CmdSet2, (int)PkgCmdIDList.cmdidMyTool2);
应该是:

 CommandID toolwndCommandID2 = new CommandID(GuidList.guidVSPackage1CmdSet, (int)PkgCmdIDList.cmdidMyTool2);
您需要修复尚未发布的.vsct文件和Guids.cs文件

也就是说,一个包只有一个命令集,可以有多个命令

FWIW,我正在编写一个关于创建工具窗口的教程。这是:

如何:在Visual Studio包中使用ToolWindowPane类创建toolwindow

查看问题出在哪里?1) 第二个toolwindow的命令/按钮未出现在“视图>其他窗口”中2)单击按钮创建toolwindow会导致“无法创建窗口”异常3)显示toolwindow但没有托管控件4)…我会避免在网上发布
Guid
s;因为我听说有人将在上找到的代码复制/粘贴到他们的项目中(-: