将两个或多个工具窗口添加到同一个Visual Studio项目中

时间:2015-02-20 02:55:25

标签: 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.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;
        }
    }
}

然后我修改了几次从Package继承的类,但是我做错了或丢失了

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中添加多个工具窗口

1 个答案:

答案 0 :(得分:3)

这肯定是错误的:

 CommandID toolwndCommandID2 = new CommandID(GuidList.guidVSPackage1CmdSet2, (int)PkgCmdIDList.cmdidMyTool2);

应该是:

 CommandID toolwndCommandID2 = new CommandID(GuidList.guidVSPackage1CmdSet, (int)PkgCmdIDList.cmdidMyTool2);

你需要修复你尚未发布的.vsct文件和Guids.cs文件。

也就是说,一个包有一个命令集,它可以有几个命令。

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

HOWTO:在Visual Studio包中使用ToolWindowPane类创建一个工具窗口 http://www.visualstudioextensibility.com/2015/02/20/mz-tools-articles-series-howto-create-a-toolwindow-with-a-toolwindowpane-class-in-a-visual-studio-package/