簡體   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