【问题标题】:How to add more than one menu item in Visual Studio 2012 using VSPackage?如何使用 VSPackage 在 Visual Studio 2012 中添加多个菜单项?
【发布时间】:2014-01-16 15:34:25
【问题描述】:

我研究了这个link,以便在 Visual Studio 2012 中使用 VSPackage 添加自定义菜单命令。

一切都很好,直到我想添加多个菜单项。

在提供的文档中,他们没有解释如何再添加一个菜单项。

比如我在TfsUtility.vsct文件中做了如下设置:

<Menus>
    <Menu guid="guidTfsUtilityCmdSet" id="TfsUtility" priority="0x700" type="Menu">
        <Parent guid="guidSHLMainMenu" id="IDG_VS_MM_TOOLSADDINS" />
        <Strings>
            <ButtonText>Tfs Utility</ButtonText>
            <CommandName>Tfs Utility</CommandName>
        </Strings>
    </Menu>
</Menus>

...

<Group guid="guidTfsUtilityCmdSet" id="MyMenuGroup" priority="0x0600">
    <Parent guid="guidTfsUtilityCmdSet" id="TfsUtility"/>
</Group>

...

<Button guid="guidTfsUtilityCmdSet" id="cmdidMyCommand" priority="0x0100" type="Button">
    <Parent guid="guidTfsUtilityCmdSet" id="MyMenuGroup" />
    <Strings>
        <ButtonText>Branch</ButtonText>
    </Strings>
</Button>

...

<GuidSymbol name="guidTfsUtilityCmdSet" value="{d5549d5d-47af-40e0-a7e5-e9ed7f64d577}">

    <IDSymbol name="MyMenuGroup" value="0x1020" />
    <IDSymbol name="cmdidMyCommand" value="0x0100" />

    <IDSymbol name="TfsUtility" value="0x1021"/>
</GuidSymbol>

问题:

  1. 如何在菜单选项中添加更多选项(例如在分支之后)?

  2. 如何为每个菜单项关联命令?我想为每个菜单项显示不同的消息。

非常感谢

【问题讨论】:

    标签: c# visual-studio-2012 sdk


    【解决方案1】:
    1. 要再添加一个菜单项,请再添加一个具有不同 ID 的 Button 部分。

    2. 要实现命令,您需要提供 MenuItemCallback 并将其注册到 OleMenuCommandService:

    OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

    if ( null != mcs )
    {
       // Create the command for the menu item.
       CommandID menuCommandID = new CommandID(guidCommandGroup, myCommandID);
       MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID );
       mcs.AddCommand( menuItem );
    }
    

    更多信息请参见Command Implementation

    【讨论】:

      【解决方案2】:

      我找到了解决办法:

      1. PkgCmdIDList 页面中,添加一些常量,例如:

        public const uint cmdiChangesets = 0x101; // or cmdiYourMenu instead of cmdiChangesets
        public const uint cmdidCheckin = 0x102;
        public const uint cmdidGet = 0x103;
        
      2. 为点击事件创建回调(在 YourProjectNamePackage 中,对我来说是 TfsUtilityPackage)

        private void TfsMenuItemBranchCallback(object sender, EventArgs e)
        {
            // do something
        }
        
        private void TfsMenuItemPendingCallback(object sender, EventArgs e)
        {
            // do something
        }
        
        ...
        
      3. 在 XXUtility.vsct(对我来说是 TfsUtility.vsct)中,在 &lt;Buttons&gt; 节点中创建按钮

                <Button guid="guidTfsUtilityCmdSet" id="cmdidChangesets" priority="0x0101" type="Button">
                    <Parent guid="guidTfsUtilityCmdSet" id="MyMenuGroup" />
                    <Strings>
                        <ButtonText>Changesets</ButtonText>
                    </Strings>
                </Button>
        
                <Button guid="guidTfsUtilityCmdSet" id="cmdidCheckin" priority="0x0102" type="Button">
                    <Parent guid="guidTfsUtilityCmdSet" id="MyMenuGroup" />
                    <Strings>
                        <ButtonText>Pending changes</ButtonText>
                    </Strings>
                </Button>
        
      4. &lt;GuidSymbol&gt;节点中添加IDSymbol

        <IDSymbol name="cmdidChangesets" value="0x0101" /> <IDSymbol name="cmdidCheckin" value="0x0102" />

      0x0101 的值必须与 PkgCmdIDList 类中的常量值 0x101 匹配,并且该值必须不同才能显示所有菜单项

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多