Outlook 上下文菜单项单击多次被触发

发布于 2025-01-04 22:09:28 字数 2105 浏览 2 评论 0原文

此处已提出此问题,但我只是对给出的答案不满意。

我目前正在向 Outlook 添加自定义上下文菜单。代码如下:

void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, Microsoft.Office.Interop.Outlook.Selection Selection)
    {
        if (Online)
        {
            foreach (string category in FilingRuleManager.FilingRuleCategories)
            {
                Office.CommandBarPopup cb = CommandBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, missing, true) as Office.CommandBarPopup;
                cb.BeginGroup = true;
                cb.Visible = true;
                cb.Tag = MENUNAME;
                cb.Caption = category;
                //now add the filing rules as a sub menu
                foreach (FilingRuleDB rule in FilingRuleManager.FilingRules.Values)
                {
                    if (rule.RuleCategory == category)
                    {
                        Office.CommandBarButton cbSub = cb.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton;
                        _FilingRules.Add(cbSub);
                        cbSub.Visible = true;
                        cbSub.Caption = rule.RuleName;
                        cbSub.Tag = rule.FilingRuleID.ToString();
                        cbSub.Click += new Office._CommandBarButtonEvents_ClickEventHandler(FilingRules_Click);
                    }
                }
            }
        }
    }

当我运行应用程序时,每次在 Outlook 中显示上下文菜单时,Click 处理程序 (FilingRules_Click) 都会被触发多次。因此,如果我右键单击 3 次,则处理程序将执行 3 次,依此类推。

必须有一种比上面链接问题中的 hack 更好的方法来实现这一点。

我尝试过:

  1. 在添加 CommandBarButtons 之前删除它们 - 但它们不存在!因为每次隐藏 Outlook 上下文菜单时,自定义项目都会自动删除。
  2. 将控件存储在列表中,然后尝试删除处理程序 - 这会产生 AV,因为隐藏菜单后按钮不再存在。
  3. 没有 ItemContextMenuHidden() 事件可供我挂钩,否则我会尝试这样做。
  4. 在加载项启动时添加项目(即,仅在没有 ItemContextMenuDisplay() 处理程序的情况下添加一次),但是这些项目永远不会出现,因为它们在显示菜单时总是被清除。

有人还有其他建议吗?

This has already been asked here, but I am just not satisfied with the answer given.

I am currently adding a custom context menu to Outlook. The code is as follows:

void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, Microsoft.Office.Interop.Outlook.Selection Selection)
    {
        if (Online)
        {
            foreach (string category in FilingRuleManager.FilingRuleCategories)
            {
                Office.CommandBarPopup cb = CommandBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, missing, true) as Office.CommandBarPopup;
                cb.BeginGroup = true;
                cb.Visible = true;
                cb.Tag = MENUNAME;
                cb.Caption = category;
                //now add the filing rules as a sub menu
                foreach (FilingRuleDB rule in FilingRuleManager.FilingRules.Values)
                {
                    if (rule.RuleCategory == category)
                    {
                        Office.CommandBarButton cbSub = cb.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton;
                        _FilingRules.Add(cbSub);
                        cbSub.Visible = true;
                        cbSub.Caption = rule.RuleName;
                        cbSub.Tag = rule.FilingRuleID.ToString();
                        cbSub.Click += new Office._CommandBarButtonEvents_ClickEventHandler(FilingRules_Click);
                    }
                }
            }
        }
    }

When i run the application, each time i show the context menu in Outlook the Click handler (FilingRules_Click) is fired that many times. So if i right click 3 times, the handler is executed 3 times and so on.

There must be a better way to acheive this than the hack in the question linked above.

I have tried:

  1. Removing the CommandBarButtons just before adding them - however they do not exist!! as every time the Outlook Context menu is hidden, the custom items are automatically removed.
  2. Storing the controls in a List and then attempting to remove the handlers - this gives an AV as the buttons no longer exist after the menu is hidden.
  3. There is no ItemContextMenuHidden() event for me to hook into otherwise i would have tried that.
  4. Adding the items when the Add-in starts (i.e. only once with no ItemContextMenuDisplay() handler), however the items never appear due to the fact that they always get cleared when the menu is shown.

Anyone out there have another suggestion?

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

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

发布评论

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

评论(1

离笑几人歌 2025-01-11 22:09:28

解决了问题。

  1. 将命令按钮定义为
  2. Outlook.Application 的类级静态变量

    附件事件 ContextMenuClose

    outlookInstance.ContextMenuClose += new ApplicationEvents_11_ContextMenuCloseEventHandler(outlookInstance_ContextMenuClose);
    
  3. 将方法实现为代码

    void OutlookInstance_ContextMenuClose(OlContextMenu ContextMenu)
    {
        if (ContextMenu == OlContextMenu.olItemContextMenu)
        {
            ContextIndexButton.Click -= new _CommandBarButtonEvents_ClickEventHandler(<你的方法>);
            上下文索引按钮 = null;
        }
    }
    

Solved the problem.

  1. define the commandbutton as class level static variable
  2. attachment event ContextMenuClose for Outlook.Application.

    outlookInstance.ContextMenuClose += new ApplicationEvents_11_ContextMenuCloseEventHandler(outlookInstance_ContextMenuClose);
    
  3. implement the method as code

    void outlookInstance_ContextMenuClose(OlContextMenu ContextMenu)
    {
        if (ContextMenu == OlContextMenu.olItemContextMenu)
        {
            ContextIndexButton.Click -= new _CommandBarButtonEvents_ClickEventHandler(<your method>);
            ContextIndexButton = null;
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文