1

Microsoft Outlookアドインを開発しています。ここでは、[アドイン]タブ名に1つのボタンを追加しましたOPENISMS。ボタンは見えましたが、クリックしてもイベントは発生しません。なぜこのように動作しているのかわかりません。以下は、ボタンを追加してイベントをアタッチするためのコードです。どんな助けでも大歓迎です。

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

イベントAddButtonToNewDropdown()からメソッドを呼び出しています。ThisAddIn_Startup

4

1 に答える 1

4

クラスメンバー変数をスコープ内に作成する必要がありCommandBarButtonます-そうしないと、ガベージコレクションが行われ、観察したようにイベントが発生しません。

public class ThisAddIn
{
   Office.CommandBarButton commandBarButton;

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

同様の問題に関する関連する SO 投稿を参照してください。

于 2013-01-03T14:10:26.037 に答える