繁体   English   中英

如何以编程方式打开Visual Studio扩展的工具窗口?

[英]How to open a tool window of a visual studio extension programmatically?

因此,我的Visual Studio扩展(程序包)中有两个工具窗口 ,我想通过第一个窗口上的按钮打开第二个窗口。

我希望在这里进行解释: “如何:以编程方式打开工具窗口” ,但事实并非如此。

您应该使用Package.FindToolWindowIVsUIShell.FindToolWindow查找或创建工具窗口。

如果从您自己的包中使用(或者您有对该包的引用,则将其放在此处而不是this ):

private void OpenFromPackage()
{
    ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true); // True means: crate if not found. 0 means there is only 1 instance of this tool window
    if (null == window || null == window.Frame)
        throw new NotSupportedException("MyToolWindow not found");

    IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
    ErrorHandler.ThrowOnFailure(windowFrame.Show());
}

如果您无法从软件包中执行此操作,或者无法对其进行引用,请使用IVSUIShell:

private void OpenWithIVsUIShell()
{
    IVsUIShell vsUIShell = (IVsUIShell)Package.GetGlobalService(typeof(SVsUIShell));
    Guid guid = typeof(MyToolWindow).GUID;
    IVsWindowFrame windowFrame;
    int result = vsUIShell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fFindFirst, ref guid, out windowFrame);   // Find MyToolWindow

    if (result != VSConstants.S_OK)
        result = vsUIShell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fForceCreate, ref guid, out windowFrame); // Crate MyToolWindow if not found

    if (result == VSConstants.S_OK)                                                                           // Show MyToolWindow
        ErrorHandler.ThrowOnFailure(windowFrame.Show());
 }

当创建具有工具窗口支持的新程序包时,您将获得一个工具窗口和一个显示它的命令。 此命令在包类中使用ShowToolWindow方法处理。

进行检查,您将看到基本包对象具有FindToolWindow方法,可用于查找(并根据需要创建)在包中实现的任何工具窗口。 这FindToolWindow方法仅仅是围绕着一个漂亮的包装IVsUIShell.FindToolWindow方法,这是显示任何工具窗口什么时候最终被调用。

因此,我建议不要使用内置在实际包对象中的较低级别的服务,而不要使用旧的EnvDTE自动化接口。

这是我的解决方法,下面的代码是第一个窗口上按钮的代码隐藏方法:

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var dte = Package.GetGlobalService(typeof(DTE)) as DTE;
            if (dte == null) return;

            var window = dte.Windows.Item("{WindowGUID}");
            window.Visible = true;
        }

您应该在Guids类以及ToolWindow类的上方找到“ WindowGUID ”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM