【发布时间】:2020-05-09 15:43:12
【问题描述】:
在我的 VSTO 插件中,我使用以下类成员来存储指向 currentExplorer、currentAppointmentItem 和 currentExplorers 的指针。
在 Startup 中,我尝试设置所有必要的事件处理程序,如下所示:
currentExplorers = this.Application.Explorers;
foreach (Outlook.Explorer explorer in currentExplorers)
{
((Outlook.ExplorerEvents_10_Event)explorer).Activate +=
new Outlook.ExplorerEvents_10_ActivateEventHandler(
Explorer_Activate);
explorer.Deactivate += new
Outlook.ExplorerEvents_10_DeactivateEventHandler(
Explorer_Deactivate);
}
currentExplorers.NewExplorer += New_Explorer;
我的事件处理程序如下所示:
void New_Explorer(Outlook.Explorer explorer)
{
if (currentExplorer != null)
{
Marshal.ReleaseComObject(currentExplorer);
}
currentExplorer = explorer;
currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Selection_Change);
currentExplorer.Deactivate += new Outlook.ExplorerEvents_10_DeactivateEventHandler(Explorer_Deactivate);
}
void Explorer_Deactivate()
{
if (currentExplorer != null)
{
currentExplorer.SelectionChange -= new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Selection_Change); ;
Marshal.ReleaseComObject(currentExplorer);
currentExplorer = null;
}
}
void Explorer_Activate()
{
if (currentExplorer != null)
{
Marshal.ReleaseComObject(currentExplorer);
}
currentExplorer = this.Application.ActiveExplorer();
currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Selection_Change);
}
private void Selection_Change()
{
if (currentExplorer == null)
{
return;
}
try
{
Outlook.MAPIFolder selectedFolder = currentExplorer.CurrentFolder;
if (currentExplorer.Selection.Count > 0)
{
Object selObject = currentExplorer.Selection[1];
if (selObject is Outlook.AppointmentItem)
{
if (currentAppointmentItem != null)
{
Marshal.ReleaseComObject(currentAppointmentItem);
}
currentAppointmentItem = (Outlook.AppointmentItem)selObject;
}
else
{
currentAppointmentItem = null;
}
}
} catch(Exception ex)
{
log.Error(ex.Message);
}
}
我在每个事件处理程序上设置断点 问题是当我调试我的 Outlook 插件时,除了 Explorer_Deactivate 之外,没有任何事件处理程序被调用。当我在那里调试时,我看到 currentExplorer 仍然为空,所以我认为它在 Outlook 初始化期间由于某种原因被调用(当时只有 Outlook 的初始屏幕可见)
我做错了什么? 我原以为任何项目(在邮件、日历等中)的每一个选择都会调用 Selection_Change,但不幸的是,情况并非如此
【问题讨论】:
标签: vsto outlook-addin