VS2010 Add-In,向上下文菜单添加命令?

发布于 2024-09-26 06:56:35 字数 2315 浏览 0 评论 0原文

我知道已经有一些关于此的线程,但我只是不会为我工作。

我想要什么: 我需要在 Visual Studio 源代码管理资源管理器的上下文菜单中添加一个新条目。为此,我启动了一个新的插件项目。

我用的是: 我使用这篇文章作为指南。 http:// /blogs.msdn.com/b/team_foundation/archive/2010/06/24/extending-work-item-tracking-context-menus.aspx

什么不起作用: 我没有遇到任何例外,无论我将其添加到何处,菜单都不会显示。

一些代码片段:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
   AddCommandToContextMenu(
                         "Team Project", // context menu Name
                         "ClearQuery", // menu reference name
                         "Clear", // display name
                         47, // command icon
                         1);    // command placement, 1= first item on top
                }
}

我使用“团队项目”菜单名称进行测试。 VSIPLogging 告诉我,如果我右键单击我们的 TFS 团队项目,这就是菜单的名称。我也尝试过其他菜单但没有成功。

以下是 AddCommandToContextMenu 函数:

private void AddCommandToContextMenu(string menuName, string commandName, string commandText, int iconId, int position)
    {

                    CommandBar contextMenu = ((CommandBars)_applicationObject.CommandBars)[menuName];

                    AddCommand(contextMenu, commandName, commandText, iconId, position);
    }



private void AddCommand(CommandBar parent, string commandName, string commandText, int iconId, int position)

    {
                     Commands2 commands = (Commands2)_applicationObject.Commands;
                    //create the command
                    Command newCommand = commands.AddNamedCommand2(_addInInstance, commandName, commandText, commandText, true, iconId);
                    // add it to parent menu
                    newCommand.AddControl(parent, position);
    }

如果我仔细观察一下,命令栏“parent”给了我相当多的异常:

accChildCount = 'parent.accChildCount' 引发了类型为 'Microsoft.VisualStudio.PlatformUI.Automation.DeprecatedException' 的

异常对于所有其他“acc”值都相同。

现在我真的不知道我做错了什么,也不知道我还能尝试什么来让这项工作成功。我想要做的就是在源代码管理资源管理器中有一个上下文菜单条目,它应该调用电源工具命令行exe来调用它的“撤消未更改”功能。

I know there are already some threads about this, but I just won't work for me.

What I want:
I need a new entry in a context menu of the Visual Studio Source Control Explorer. For this I started a new Add-In Project.

What I used:
I used this article as a guide.
http://blogs.msdn.com/b/team_foundation/archive/2010/06/24/extending-work-item-tracking-context-menus.aspx

What is not working:
I don't get any exceptions, the menu just won't show up, no matter where I add it.

Some code snippets:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
   AddCommandToContextMenu(
                         "Team Project", // context menu Name
                         "ClearQuery", // menu reference name
                         "Clear", // display name
                         47, // command icon
                         1);    // command placement, 1= first item on top
                }
}

I am using "Team Project" menu name for testing. VSIPLogging tells me, that this is the name of the menu if I make a right click on our TFS Team Project. I also tried other menus without success.

Here are the AddCommandToContextMenu functions:

private void AddCommandToContextMenu(string menuName, string commandName, string commandText, int iconId, int position)
    {

                    CommandBar contextMenu = ((CommandBars)_applicationObject.CommandBars)[menuName];

                    AddCommand(contextMenu, commandName, commandText, iconId, position);
    }



private void AddCommand(CommandBar parent, string commandName, string commandText, int iconId, int position)

    {
                     Commands2 commands = (Commands2)_applicationObject.Commands;
                    //create the command
                    Command newCommand = commands.AddNamedCommand2(_addInInstance, commandName, commandText, commandText, true, iconId);
                    // add it to parent menu
                    newCommand.AddControl(parent, position);
    }

The commandbar "parent" gives me quite some exceptions, if I take a closer look at it:

accChildCount = 'parent.accChildCount' threw an exception of type 'Microsoft.VisualStudio.PlatformUI.Automation.DeprecatedException'

The same for every other "acc" value.

Now I really don't know what I did wrong or what else I could try to make this work. All I want to do is to have a context menu entry in the source control explorer, which should call the power tools command line exe to call the "undo unchanged" function of it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

貪欢 2024-10-03 06:56:35

我非常确定 Visual Studio 中的弹出窗口是 CommnadBarPopup 类型。
我非常确定的另一件事是,您需要使命令/控件全局化,以便保留它们的引用,否则 GC 将杀死它们。

您需要确保 AddCommand 中的命令名称不包含点,而在 Query / Exec 函数中则包含点,例如:

newCommand = commands.AddNamedCommand2(_addInInstance, commandName, commandText, commandText, true, iconId, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,(int)vsCommandStyle.vsCommandStylePictAndText,vsCommandControlType.vsCommandControlTypeButton);

这里需要注意的几件事:

  1. newCommand 不是代码中的局部变量,它被提升到一个全局变量以使其保持活动状态(无论如何,这可能不是你的情况,如果这是问题 - 你会第一次看到它,然后它就会消失)。
  2. 你省略了参数,这里的ref ContextGUIDS是一个新的object[],在方法调用之前声明,用于保存命令的guid,这并不重要,只需添加它,重要的是接下来的参数,第一个一个告诉 Visual Studio 该命令是否可见并已启用: (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled ,下一个给出一些关于命令应该执行的提示看起来像(在我们的例子中是按钮)。

这只是一个起点,请参考这篇文章:
如何:使用加载项中的 Visual Studio 命令栏弹出窗口创建上下文菜单

祝你好运!

I am pretty sure the Popups in Visual Studio were of CommnadBarPopup type.
The other thing I am pretty sure was that you need to make your commands / controls global so a reference is kept on them, otherwise GC will kill them.

You need to make sure that the command name in the AddCommand doesn't contain dots, and in the Query / Exec functions it does, e.g.:

newCommand = commands.AddNamedCommand2(_addInInstance, commandName, commandText, commandText, true, iconId, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,(int)vsCommandStyle.vsCommandStylePictAndText,vsCommandControlType.vsCommandControlTypeButton);

Few things to note here:

  1. newCommand is not a local variable as in your code, it is promoted to a global variable to keep it alive (anyway, this is not your case probably, if this was the problem - you would see it the first time and then it will disappear).
  2. You ommited parameters, the ref ContextGUIDS here is a new object[] that was declared just before the method call to hold the guid for the command, it is not that important, just add it, what is important are the next parameters, the first one tells visual studio if the command is visible and enabled : (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled and the next give some hint on what your command should look like (button in our case).

This is just a start point, plaese refer to this article:
HOWTO: Create a context menu using a Visual Studio commandbar popup from an add-in

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文