Outlook Context菜单项点击多次触发 [英] Outlook Context Menu item click fired multiple times

查看:228
本文介绍了Outlook Context菜单项点击多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经被问到这里,但我只是不满意给出的答案。



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

  void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar,Microsoft.Office.Interop.Outlook。选择选择)
{
if(Online)
{
foreach(FilingRuleManager.FilingRuleCategories中的字符串类别)
{
Office.CommandBarPopup cb = CommandBar。 Control.Cont
cb.BeginGroup = true;
cb.Visible = true;
cb.Tag = MENUNAME;
cb.Caption = category;
//现在添加归档规则作为子菜单
foreach(FilingRuleManager.FilingRules.Values中的FilingRuleDB规则)
{
if(rule.RuleCategory == category)
{
Office.CommandBarButton cbSub = cb.Controls.Add(Office.MsoControlType.msoControlButton,缺少,缺少,缺少,真实)为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中显示上下文菜单时,点击处理程序(FilingRules_Click)被触发了很多次。所以,如果我右键单击3次,则处理程序将执行3次,以此类推。



与上面链接的问题中的黑客相比,必须有一个更好的方法来实现



我尝试过:


  1. 在添加CommandBarButton之前,然而他们不存在!每当Outlook Context菜单被隐藏时,自定义项目将被自动删除。

  2. 将控件存储在列表中,然后尝试删除处理程序 - 这将给出AV作为按钮无

  3. 没有ItemContextMenuHidden()事件可以挂钩,否则我会尝试过。

  4. 添加当加载项启动时的项目(即只有一次没有ItemContextMenuDisplay()处理程序),但是项目不会出现,因为它们总是在显示菜单时被清除。

  5. 解决方案

    解决问题。 / p>


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

    2. 附件事件 ContextMenuClose for Outlook.Application

        outlookInstance.ContextMenuClose + = new ApplicationEvents_11 _ContextMenuCloseEventHandler(outlookInstance_ContextMenuClose); 


    3. 实现方法作为代码

        void outlookInstance_ContextMenuClose(OlContextMenu ContextMenu)
      {
      if(ContextMenu == OlContextMenu.olItemContextMenu)
      {
      ContextIndexButton.Click - = new _CommandBarButtonEvents_ClickEventHandler(< your method>);
      ContextIndexButton = null;
      }
      }



    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?

    解决方案

    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;
          }
      }
      

    这篇关于Outlook Context菜单项点击多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆