2012-12-02 60 views
3

我使用的是包裝類此屬性暴露工具窗口中VS:VSIX:添加工具窗口查看 - >其他窗口

[ProvideToolWindow(typeof(MyToolWindow), 
     Style = VsDockStyle.Tabbed, 
     Orientation = ToolWindowOrientation.Right)] 

這工作得很好,我可以以編程方式使用此代碼打開:

private void ShowToolWindow() 
    { 
    //this method will show the window if it's not active or bring it to front if it's collapsed 
    ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true); 
    if ((null == window) || (null == window.Frame)) 
    { 
     throw new NotSupportedException(); 
    } 
    IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame; 
    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show()); 
    } 

但是,當用戶不小心關閉窗口時,無法恢復它。我想知道其他擴展如何將它添加到Tools-> Other Windows,例如NuGet。我在NuGet源代碼中找不到任何明顯的東西。

+0

嘿,你能不能幫我開始使用VSIX基礎。之前我創建了一個VS加載項,現在我想將它遷移到vsix。我加載由一個工具窗口的 –

回答

3

您需要添加調用您ShowToolWindow程序的命令。

你的包的頂部vsct文件將需要幾個外部引用:

<!--This is the file that defines the IDs for all the commands exposed by VisualStudio. --> 
<Extern href="stdidcmd.h"/> 
<!--This header contains the command ids for the menus provided by the shell. --> 
<Extern href="vsshlids.h"/> 

這個文件應該定義一些符號:

<Symbols> 
    <!-- Use your package guid. --> 
    <GuidSymbol name="guidPackage" value="{00000000-0000-0000-0000-000000000000}" /> 

    <!-- Use a new GUID to uniquely identify your package commands --> 
    <GuidSymbol name="guidCmdSet" value="{11111111-1111-1111-1111-111111111111}"> 
     <IDSymbol name="cmdidViewMyToolWindow" value="0x0500" /> 
    </GuidSymbol> 
</Symbols> 

在你的包的vsct文件Buttons塊,加像這樣:

<Button guid="guidCmdSet" id="cmdidViewMyToolWindow" priority="0x0100" type="Button"> 
    <!--IDG_VS_WNDO_OTRWNDWS0 is the first group in "View|Other Windows". See 
    C:\Program Files (x86)\Microsoft Visual Studio 2010 SDK SP1\VisualStudioIntegration\Common\Inc 
    for other options. --> 
    <Parent guid="guidSHLMainMenu" id="IDG_VS_WNDO_OTRWNDWS0"/> 
    <CommandFlag>DynamicVisibility</CommandFlag> 
    <CommandFlag>DefaultInvisible</CommandFlag> 
    <Strings> 
     <ButtonText>View &amp;My Tool Window</ButtonText> 
    </Strings> 
</Button> 

所有這一切應該使「查看我的T ool Window「出現在視圖菜單的頂部。現在,當有人點擊它時,你需要採取行動。

你的包應該實現IOleCommandTarget處理命令的知名度和實現:

public class MyPackage : Package, IOleCommandTarget 
{ 
    #region IOleCommandTarget implementation 

    /// <summary> 
    /// The VS shell calls this function to know if a menu item should be visible and 
    /// if it should be enabled/disabled. 
    /// This is called only when the package is active. 
    /// </summary> 
    /// <param name="guidCmdGroup">Guid describing which set of commands the current command(s) belong to</param> 
    /// <param name="cCmds">Number of commands for which status are being asked</param> 
    /// <param name="prgCmds">Information for each command</param> 
    /// <param name="pCmdText">Used to dynamically change the command text</param> 
    /// <returns>HRESULT</returns> 
    public int QueryStatus(ref Guid guidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText) 
    { 
     // Filter out commands that are not defined by this package 
     if (guidCmdGroup != new Guid("{00000000-0000-0000-0000-000000000000}")) 
      return (int)(Constants.OLECMDERR_E_NOTSUPPORTED); 

     if (cCmds == 0 || prgCmds == null || prgCmds.Length == 0 || cCmds != prgCmds.Length) 
      return VSConstants.E_INVALIDARG; 

     // Show and enable all commands. 
     OLECMDF cmdf = OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED; 
     for (int i = 0; i < cCmds; i++) 
      prgCmds[i].cmdf = (uint)cmdf; 

     return VSConstants.S_OK; 
    } 

    #endregion 
} 

最後,在你的包初始化程序,您告訴shell點擊您的命令時,該怎麼做:

protected override void Initialize() 
{ 
    base.Initialize(); 

    // Add our command handlers (commands must exist in the .vsct file) 
    OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; 
    if (null != mcs) 
    { 
     // "View My Tool Window" command callback 
     CommandID menuCommandID = new CommandID(new Guid("{11111111-1111-1111-1111-111111111111}"), (int)0x500); 
     MenuCommand menuItem = new MenuCommand(ShowToolWindow, menuCommandID); 
     mcs.AddCommand(menuItem); 
    } 
} 

在「真實」的代碼,你可能要定義在C#中一些GUID常數匹配在vsct定義的符號是,和整個使用這些。

+0

謝謝,這是我的擴展代碼審查的最後一位 - IronBoard :) –