单击自定义按钮不会触发Outlook事件 [英] Outlook Event is not getting fired on click of custom button
问题描述
我正在开发Microsoft Outlook加载项,在其中我在加载项"选项卡名称OPENISMS
中添加了一个按钮.我可以看到该按钮,但是单击该事件并不会触发.我不知道为什么它会以这种方式表现.请在下面找到用于添加按钮并为其添加事件的代码.任何帮助将不胜感激.
I am developing a Microsoft Outlook Add-in, where I have added one button in Add-In tab name OPENISMS
. I could see the button, however on click the event is not getting fired. I have no clue why it is behaving in this manner. Please find below are code for adding button and attaching event to it. Any help will be highly appreciated.
private void AddButtonToNewDropdown()
{
Office.CommandBar commandBar = this.Application.ActiveExplorer().CommandBars["Standard"];
Office.CommandBarControl ctl = commandBar.Controls["&New"];
if (ctl is Office.CommandBarPopup)
{
Office.CommandBarButton commandBarButton;
Office.CommandBarPopup newpopup = (Office.CommandBarPopup)ctl;
commandBarButton = (Office.CommandBarButton)newpopup.Controls.Add(1, missing, missing, missing, true);
commandBarButton.Caption = "OpenISMS";
commandBarButton.Tag = "OpenISMS";
commandBarButton.FaceId = 6000;
//commandBarButton.Enabled = false;
commandBarButton.OnAction = "OpenISMSThruMail.ThisAddIn.ContextMenuItemClicked";
commandBarButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ContextMenuItemClicked);
}
}
private void ContextMenuItemClicked(CommandBarButton Ctrl, ref bool CancelDefault)
{
if (currentExplorer.Selection.Count > 0)
{
object selObject = currentExplorer.Selection[1];
if (selObject is MailItem)
{
// do your stuff with the selected message here
MailItem mail = selObject as MailItem;
MessageBox.Show("Message Subject: " + mail.Subject);
}
}
}
我正在从ThisAddIn_Startup
事件中调用AddButtonToNewDropdown()
方法.
I am calling AddButtonToNewDropdown()
method from ThisAddIn_Startup
event.
推荐答案
您需要将CommandBarButton
设置为作用域中的class-member变量-否则它将被垃圾回收并且事件不会像您观察到的那样触发
You need to make the CommandBarButton
a class-member variable in scope - otherwise it will be garbage collected and the event will not fire as you've observed.
public class ThisAddIn
{
Office.CommandBarButton commandBarButton;
private void AddButtonToNewDropdown()
{
// ...
}
}
有关类似问题,请参见相关的SO帖子.
See related SO post regarding similar issue.
这篇关于单击自定义按钮不会触发Outlook事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!