Visual Studio 添加上下文菜单不接收事件
我正在尝试一个视觉工作室插件,并遇到了非常奇怪的行为。我正在添加右键单击上下文菜单项,但除非我在 OnConnect 方法中放置断点并等待一会儿,否则我不会触发事件。我所做的几乎与
这是一个完全标准的插件,包含以下代码:
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
applicationObject = (DTE2)application;
addInInstance = (AddIn)addInInst;
var commandBars = ((CommandBars)applicationObject.CommandBars)["Code Window"];
var popup = commandBars.Controls.Add(MsoControlType.msoControlButton,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value, 1, true);
popup.Caption = "View graph";
var commandBarEvents = (CommandBarEvents)(applicationObject.Events.CommandBarEvents[popup]);
commandBarEvents.Click += ViewGraphClick;
}
private void ViewGraphClick(object commandbarcontrol, ref bool handled, ref bool canceldefault)
{
var selection = (TextSelection)applicationObject.ActiveDocument.Selection;
selection.Insert("Hello, world!");
handled = true;
}
如果我在方法的早期使用断点等待,则该插件将起作用。如果我不等待,它永远不会调用我的事件。然而,在所有情况下它都会将我的项目添加到上下文菜单中。
据我所知,这个方法只被调用一次。关于我在这里做错了什么有什么想法吗?
更新:我尝试将相同的初始化代码放入OnStartup
中。结果是一样的。我也尝试按照此处所示的示例进行操作,但是我的 < code>OnConnection 仅在 connectMode
设置为 Startup
时被调用一次。
更新 2 我已经尝试了尽可能多的组合,移动了所有初始化。最终,我放弃并开始使用另一个示例,该示例使用命令而不是简单的上下文菜单。从长远来看,这可能是一个更好的解决方案,但我仍然对为什么原始代码不起作用感兴趣。
I'm experimenting with a visual studio addon and have come across very odd behaviour. I'm adding a right click context menu item and I'm not getting the events to fire unless I put a breakpoint in the OnConnect
method and wait a little while. I'm doing pretty much exactly as in this article.
It's a totally standard add-in with this code:
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
applicationObject = (DTE2)application;
addInInstance = (AddIn)addInInst;
var commandBars = ((CommandBars)applicationObject.CommandBars)["Code Window"];
var popup = commandBars.Controls.Add(MsoControlType.msoControlButton,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value, 1, true);
popup.Caption = "View graph";
var commandBarEvents = (CommandBarEvents)(applicationObject.Events.CommandBarEvents[popup]);
commandBarEvents.Click += ViewGraphClick;
}
private void ViewGraphClick(object commandbarcontrol, ref bool handled, ref bool canceldefault)
{
var selection = (TextSelection)applicationObject.ActiveDocument.Selection;
selection.Insert("Hello, world!");
handled = true;
}
If I wait with a breakpoint early in the method, this addon will work. If I don't wait it will never call my event. It will however add my item to the context menu in all cases.
As far as I can see this method only gets called once. Any ideas on what I'm doing wrong here?
Update: I've tried to put the same initialization code in OnStartup
. The result is the same. I've also tried to do as per the example shown here, but my OnConnection
only gets called once with the connectMode
set to Startup
.
Update 2 I've tried as many combinations of this as possible, moving all the initialization around. Eventually, I gave up and started using another example which uses commands instead of plain context menus. This is probably a better solution in the long run, but I'm still interested in why the original code didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使这些变量成为全局变量:
如果您在 OnConnection 中创建这些对象,那么一旦代码超出范围,它们就会被释放。
Make these variables global:
If you create these objects in OnConnection, they are disposed as soon as the code goes out of scope.