VS2010 Add-In,向上下文菜单添加命令?
我知道已经有一些关于此的线程,但我只是不会为我工作。
我想要什么: 我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我非常确定 Visual Studio 中的弹出窗口是 CommnadBarPopup 类型。
我非常确定的另一件事是,您需要使命令/控件全局化,以便保留它们的引用,否则 GC 将杀死它们。
您需要确保 AddCommand 中的命令名称不包含点,而在 Query / Exec 函数中则包含点,例如:
这里需要注意的几件事:
这只是一个起点,请参考这篇文章:
如何:使用加载项中的 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.:
Few things to note here:
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!