【问题标题】:Add two or more Tool Window into the same Visual Studio project将两个或多个工具窗口添加到同一个 Visual Studio 项目中
【发布时间】:2015-02-20 02:55:25
【问题描述】:

我试图在 Visual Studio 中向 VSPackage 项目添加第二个工具窗口,我有一个项目,其中在创建 VSPackage 项目时使用 Visual Studio 提供的向导创建了一个工具窗口,我正在网上冲浪寻找一些教程可以帮助我向现有的 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)第二个工具窗口的命令/按钮未出现在视图>其他窗口中 2)单击按钮创建工具窗口会导致“无法创建窗口”异常 3)显示工具窗口但没有托管控件 4).. ..
  • 我会避免在网上发布Guids;因为我听说有人将在 SO 上找到的代码复制/粘贴到他们的项目中(-:

标签: c# visual-studio-2013 visual-studio-extensions vspackage


【解决方案1】:

这肯定是错的:

 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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多