How to open a tool window of a visual studio exten

2019-07-24 16:41发布

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.

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-24 17:25

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

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());
}

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());
 }
查看更多
在下西门庆
3楼-- · 2019-07-24 17:25

Here's how I solved it, the following code is the code-behind method of the button on the first window:

        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;
        }

You should find the "WindowGUID" in the Guids class and above the class of the ToolWindow.

查看更多
Emotional °昔
4楼-- · 2019-07-24 17:42

When you create a new package with toolwindow support, you get a single toolwindow and a command that displays it. This command is handled in the package class with the ShowToolWindow method.

Examining that, you'll see that the base package object has a FindToolWindow method that you can use to find (and create if needed) any toolwindow you have implemented in your package. That FindToolWindow method is just a nice wrapper around the IVsUIShell.FindToolWindow method, which is what ultimately gets invoked when displaying any toolwindow.

So instead of using the old EnvDTE automation interface, I would recommend using the lower level services built into the actual package object.

查看更多
登录 后发表回答