C# VSTO Outlook插件-SelectionChangeEventHandler仅在启动时启动

C# VSTO Outlook插件-SelectionChangeEventHandler仅在启动时启动,c#,vsto,outlook-addin,C#,Vsto,Outlook Addin,我计划在每次选择outlook电子邮件时检索所选电子邮件的邮件项目,但事件句柄仅在启动时触发,有时会正确触发。我似乎找不到是什么导致了这个问题,大多数论坛导致了一个死胡同,因此这篇文章 总之,这里是我的启动方法的一个片段: private void Main_Startup(object sender, System.EventArgs e) { this.Application.ItemSend += new Outlook.ApplicationEvents_11_

我计划在每次选择outlook电子邮件时检索所选电子邮件的邮件项目,但事件句柄仅在启动时触发,有时会正确触发。我似乎找不到是什么导致了这个问题,大多数论坛导致了一个死胡同,因此这篇文章

总之,这里是我的启动方法的一个片段:

private void Main_Startup(object sender, System.EventArgs e)
    {

        this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);

        Outlook.Explorer currentExplorer = this.Application.ActiveExplorer();
        currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);

        outlookNameSpace = this.Application.GetNamespace("MAPI");
        inbox = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
        items = inbox.Items;
        items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);

    }
下面是我的CurrentExplorer\u事件方法的一个片段:

private void CurrentExplorer_Event()
    {
        newSelectedEmail = new Email();
        Outlook.MAPIFolder selectedFolder = this.Application.ActiveExplorer().CurrentFolder;
        try
        {
            if (this.Application.ActiveExplorer().Selection.Count > 0)
            {
                Object selObject = this.Application.ActiveExplorer().Selection[1];
                if (selObject is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
                    GetEmailInfoFromOutlookEmail(mailItem);
                }

            }
        }
        catch (Exception ex)
        {
            Operations.SaveLogToFile(LogType.Error, "Main - CurrentExplorer_Event", ex.Message, ex.StackTrace);
        }
    }

非常感谢您的帮助。谢谢大家!

引发事件的变量(currentExplorer)是一个局部变量。一旦它超出范围,它就有资格被垃圾收集器释放。一旦发生这种情况,就不会引发任何事件


将该变量的声明移动到类级别。

这救了我的命。非常感谢,德米特里!如果它回答了您的问题,请将其标记为这样。非常感谢。