问题描述
我试图向 Visual Studio 中的 VSPackage 项目添加第二个工具窗口,我有一个项目,其中一个工具窗口已经在创建 VSPackage 项目时使用 Visual Studio 提供的向导创建,我正在网上冲浪对于一些可以帮助我向现有 VSPackage 项目添加第二个工具窗口的教程.我已经阅读了几篇关于工具窗口的文章,但我找不到解决方案.我创建一个新类
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;
}
}
}
然后我多次修改从 Package 继承的类,但我做错或遗漏了一些事情
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);
}
}
}
}
我只想在visual studio的同一个vspackage中添加多个工具窗口
I just want to add more than one tool window in the same vspackage in visual studio
推荐答案
这肯定是错误的:
CommandID toolwndCommandID2 = new CommandID(GuidList.guidVSPackage1CmdSet2, (int)PkgCmdIDList.cmdidMyTool2);
应该是:
CommandID toolwndCommandID2 = new CommandID(GuidList.guidVSPackage1CmdSet, (int)PkgCmdIDList.cmdidMyTool2);
并且您需要修复您尚未发布的 .vsct 文件和 Guids.cs 文件.
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,我正在编写有关创建工具窗口的教程.这是:
FWIW, I am working on a tutorial about creating toolwindows. Here it is:
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/
HOWTO: Create a toolwindow with a ToolWindowPane class in a Visual Studio packagehttp://www.visualstudioextensibility.com/2015/02/20/mz-tools-articles-series-howto-create-a-toolwindow-with-a-toolwindowpane-class-in-a-visual-studio-package/
这篇关于将两个或多个工具窗口添加到同一个 Visual Studio 项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!