我试图在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中添加多个工具窗口
最佳答案
这肯定是错误的:
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/
关于c# - 将两个或多个工具窗口添加到同一Visual Studio项目中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28620932/