如何以编程方式打开 Visual Studio 扩展的工具窗口? [英] How to open a tool window of a visual studio extension programmatically?

查看:61
本文介绍了如何以编程方式打开 Visual Studio 扩展的工具窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有 两个工具windows 在我的 Visual Studio 扩展(包)中,我想通过第一个窗口上的按钮打开第二个窗口.

So I've got two tool windows in my visual studio extension (package) and I'd like to open up the second window via a button on the first window.

我希望能在这里解释:如何:以编程方式打开工具窗口",但事实并非如此.

I expected this to be explained here: "How to: Open a Tool Window Programmatically", but it wasn't.

推荐答案

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

You should use either Package.FindToolWindow or IVsUIShell.FindToolWindow to find or create a tool window.

如果从你自己的包中使用(或者如果你有一个包的引用,把它放在那里而不是this):

If used from your own package (or if you have a reference to the package, just put it there instead of 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:

If you can't do it from your package, or don't have a reference to it, use 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());
 }

这篇关于如何以编程方式打开 Visual Studio 扩展的工具窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆