Visual Studio 添加上下文菜单不接收事件

发布于 2025-01-04 11:25:24 字数 1549 浏览 1 评论 0原文

我正在尝试一个视觉工作室插件,并遇到了非常奇怪的行为。我正在添加右键单击上下文菜单项,但除非我在 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 技术交流群。

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

发布评论

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

评论(1

会傲 2025-01-11 11:25:24

使这些变量成为全局变量:

    CommandBarEvents commandBarEvents;
    CommandBar commandBars;
    CommandBarControl popup;

如果您在 OnConnection 中创建这些对象,那么一旦代码超出范围,它们就会被释放。

Make these variables global:

    CommandBarEvents commandBarEvents;
    CommandBar commandBars;
    CommandBarControl popup;

If you create these objects in OnConnection, they are disposed as soon as the code goes out of scope.

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