根据我的理解,你需要跟踪探索者的激活和停用。在激活过程中,您需要为当前的探索者添加SelectionChange事件处理程序.这似乎可以完美地用于单次点击AppointmentItems.但当双击预约系列并选择单个预约时,它使Addin崩溃。但是当双击一个预约系列并选择一个单一的预约时,Addin就会崩溃。
下面是源码:在类级
private Outlook.Explorer currentExplorer = null;
private Outlook.AppointmentItem currentAppointmentItem = null;
在Startup内。
currentExplorer = this.Application.ActiveExplorer();
((Outlook.ExplorerEvents_10_Event)currentExplorer).Activate +=
new Outlook.ExplorerEvents_10_ActivateEventHandler(
Explorer_Activate);
currentExplorer.Deactivate += new
Outlook.ExplorerEvents_10_DeactivateEventHandler(
Explorer_Deactivate);
事件处理程序
void Explorer_Activate()
{
currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Selection_Change);
}
void Explorer_Deactivate()
{
currentExplorer.SelectionChange -= new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Selection_Change); ;
}
private void Close_Explorer()
{
}
private void Selection_Change()
{
Outlook.MAPIFolder selectedFolder = currentExplorer.CurrentFolder;
if (currentExplorer.Selection.Count > 0)
{
Object selObject = currentExplorer.Selection[1];
if (selObject is Outlook.AppointmentItem)
{
currentAppointmentItem = (Outlook.AppointmentItem)selObject;
}
else
{
currentAppointmentItem = null;
}
}
}
我忽略了什么?取消注册的形式是个问题吗?
尝试在事件处理程序中添加trycatch块。Outlook对象模型有时会给你带来不可预知的结果。值得添加它们,并找到抛出异常的地方。
currentExplorer.Selection.Count
另外,你可以订阅 SelectionChange
中的事件 新探索者 事件,并且当它们被激活或停用时,不会在资源管理器之间切换。每当一个新的资源管理器窗口被打开时,该事件就会被触发,无论是用户操作的结果还是通过程序代码。
我唯一添加的是NewInspector和InspectorClose事件的处理程序,以及Marshal.ReleaseComObject()。我唯一可以想象的是,在调试时双击会出现某种竞赛条件(因为双击也会触发Selection_Change事件)。但这只是一个猜测。
你不需要添加和删除事件处理程序,因为一个探索者被激活停用了。你是想支持多个资源管理器吗?在这种情况下,创建一个包装器类,其中包含了 Explorer
对象作为其成员,并将其方法作为事件处理程序使用。