2015-04-02 66 views

回答

5

您應該使用Package.FindToolWindowIVsUIShell.FindToolWindow查找或創建一個工具窗口。

如果從自己的包中使用(或者,如果你有到包的引用,只是把它放在那裏,而不是):

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

這裏是我如何解決它,下面的代碼是按鈕的第一窗口上的代碼隱藏方法:

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

你應該找到「WindowGUID」中的GUID類及以上ToolWindow的類。

+0

DTE.Windows.Item()將'object item'作爲參數。 – 2016-05-05 13:23:00

1

當你創建一個新包與工具窗口的支持,你會得到一個單一的工具窗口,並顯示它的命令。該命令在包類中使用ShowToolWindow方法處理。

檢查,你會發現基礎包對象具有可以用來找到FindToolWindow方法(如果需要創建)的任何工具窗口你已經在你的包中實現。 FindToolWindow方法只是IVsUIShell.FindToolWindow方法的一個很好的包裝,它在顯示任何工具窗口時最終被調用。

因此,而不是使用舊EnvDTE自動化接口的,我會建議使用內置到實際的包對象的較低水平的服務。

+0

我已經爲Ed Dore的答案添加了一些代碼。見下面。 – Shakaron 2015-06-29 15:55:45

相關問題