通过 DTE 在 VS 扩展中添加菜单项不会触发 Click 处理程序
我正在尝试使用 VS 包(扩展)中的菜单项动态填充解决方案右键菜单。
我有一个名为“MyMenuName”的扩展菜单,我正在其中添加菜单项。
这是我使用的代码:
DTE2 dte = GetService(typeof (DTE)) as DTE2;
CommandBars cmdBars = dte.CommandBars as CommandBars;
CommandBar owner = cmdBars["MyMenuName"];
CommandBarControl ctl1 = owner.Controls.Add(
MsoControlType.msoControlButton, 1, string.Empty, 1, true);
ctl1.Caption = "caption";
ctl1.Enabled = true;
//register on the Click event of the
//button menu item
CommandBarEvents buttonMenuItemHandler = (CommandBarEvents)dte.DTE.Events.CommandBarEvents[ctl1];
buttonMenuItemHandler.Click += buttonMenuItemHandler_Click;
菜单项已成功添加,但它的 Click 处理程序从未执行。有什么想法吗?
编辑:我在这里挖掘了一些代码: https://netrepo.svn .codeplex.com/svn/trunk/AssemblyFactoryAddin/AssemblyFactoryAddin/GUIButton.cs 看来我的代码应该是正确的(假设他们的代码有效)
I am trying to dynamically fill the Solution right click menu with menu items from VS Package (Extension).
I have a extension menu with name "MyMenuName" and I am adding menu items there.
Here is the code I use:
DTE2 dte = GetService(typeof (DTE)) as DTE2;
CommandBars cmdBars = dte.CommandBars as CommandBars;
CommandBar owner = cmdBars["MyMenuName"];
CommandBarControl ctl1 = owner.Controls.Add(
MsoControlType.msoControlButton, 1, string.Empty, 1, true);
ctl1.Caption = "caption";
ctl1.Enabled = true;
//register on the Click event of the
//button menu item
CommandBarEvents buttonMenuItemHandler = (CommandBarEvents)dte.DTE.Events.CommandBarEvents[ctl1];
buttonMenuItemHandler.Click += buttonMenuItemHandler_Click;
Menu Item is added successfully, but it Click handler never gets executed. Any ideas?
Edit: I digged some code here: https://netrepo.svn.codeplex.com/svn/trunk/AssemblyFactoryAddin/AssemblyFactoryAddin/GUIButton.cs
And it seems that my code should be correct (assuming that their code works)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
CommandBarButton
代替使用CommandBarControl
。例如:这将解决问题。
Rather than working with
CommandBarControl
useCommandBarButton
instead. E.g.:This will fix the issue.