Outlook Context菜单项点击多次触发 [英] Outlook Context Menu item click fired multiple times
问题描述
这已经被问到这里,但我只是不满意给出的答案。
我正在向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次,以此类推。
与上面链接的问题中的黑客相比,必须有一个更好的方法来实现
我尝试过:
- 在添加CommandBarButton之前,然而他们不存在!每当Outlook Context菜单被隐藏时,自定义项目将被自动删除。
- 将控件存储在列表中,然后尝试删除处理程序 - 这将给出AV作为按钮无
- 没有ItemContextMenuHidden()事件可以挂钩,否则我会尝试过。
- 添加当加载项启动时的项目(即只有一次没有ItemContextMenuDisplay()处理程序),但是项目不会出现,因为它们总是在显示菜单时被清除。
- 将命令按钮定义为类级静态变量
-
附件事件
ContextMenuClose
forOutlook.Application
。outlookInstance.ContextMenuClose + = new ApplicationEvents_11 _ContextMenuCloseEventHandler(outlookInstance_ContextMenuClose);
-
实现方法作为代码
void outlookInstance_ContextMenuClose(OlContextMenu ContextMenu)
{
if(ContextMenu == OlContextMenu.olItemContextMenu)
{
ContextIndexButton.Click - = new _CommandBarButtonEvents_ClickEventHandler(< your method>);
ContextIndexButton = null;
}
}
- 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.
- 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.
- There is no ItemContextMenuHidden() event for me to hook into otherwise i would have tried that.
- 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.
- define the commandbutton as class level static variable
attachment event
ContextMenuClose
forOutlook.Application
.outlookInstance.ContextMenuClose += new ApplicationEvents_11_ContextMenuCloseEventHandler(outlookInstance_ContextMenuClose);
implement the method as code
void outlookInstance_ContextMenuClose(OlContextMenu ContextMenu) { if (ContextMenu == OlContextMenu.olItemContextMenu) { ContextIndexButton.Click -= new _CommandBarButtonEvents_ClickEventHandler(<your method>); ContextIndexButton = null; } }
任何人都有其他建议?
解决问题。 / p>
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:
Anyone out there have another suggestion?
Solved the problem.
这篇关于Outlook Context菜单项点击多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!