2013-01-03 90 views
1

我正在開發一個Microsoft Outlook加載項,在Add-In標籤名稱中添加了一個按鈕名稱OPENISMS。我可以看到按鈕,但點擊事件沒有被解僱。我不知道爲什麼它會以這種方式行事。請在下面找到添加按鈕並將事件附加到它的代碼。任何幫助將不勝感激。Outlook事件不會被點擊自定義按鈕

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()方法。

回答

4

您需要將CommandBarButton作爲範圍內的一個類成員變量 - 否則它將被垃圾收集,並且事件不會像您觀察到的那樣觸發。

public class ThisAddIn 
{ 
    Office.CommandBarButton commandBarButton; 

    private void AddButtonToNewDropdown() 
    { 
    // ... 
    } 
} 

請參閱related SO post regarding similar issue

+0

太好了 - 很高興它幫助你。請將此標記爲已接受的答案,以便其他人可以獲益。只需點擊投票箭頭左側的勾號(*複選標記*)即可。 – SliverNinja