我正在尝试编写一个插件,注意保存约会并执行一些数据。为此,我需要检查当活动检查器关闭时是否保存该项目。
我的问题是:当尝试将WriteEvent
- 侦听器绑定到FormRegionShowing
- 方法中的当前项时,我需要ActiveInspector
来事件获取当前项。但是,当获得ActiveInspector
时,它可能是null
,因为在活动检查器实际处于活动状态之前调用该方法。
但是,尝试在FormRegionClosed
方法中绑定它时,写入事件永远不会触发。那么,我如何知道用户实际保存AppointmentItem
的时间?
编辑:我设法在FormRegionShowing
- 方法中绑定了写事件,但它仍然不会触发:
private void ADDIN_NAME_FormRegionShowing(object sender, System.EventArgs e){
Outlook.AppointmentItem currentItem = (Outlook.AppointmentItem)this.OutlookItem;
currentItem.Write += new Outlook.ItemEvents_10_WriteEventHandler(currentItem_Write);
currentItem.AfterWrite += new Outlook.ItemEvents_10_AfterWriteEventHandler(currentItem_AfterWrite);
MessageBox.Show("added handlers");
}
void currentItem_AfterWrite(){
MessageBox.Show("item has been saved");
}
void currentItem_Write(ref bool Cancel){
MessageBox.Show("item being saved");
}
答案 0 :(得分:2)
您需要将Outlook.AppointmentItem
移动到类级别变量。在COM中,当函数超出范围时,RCW将被垃圾收集。如果要将事件与Office一起使用,则需要确保review the eventing model。另请参阅related SO post。
private Outlook.AppointmentItem currentItem; // keep events from getting GC'd
private void ADDIN_NAME_FormRegionShowing(object sender, System.EventArgs e){
currentItem = (Outlook.AppointmentItem)this.OutlookItem;
currentItem.Write += new Outlook.ItemEvents_10_WriteEventHandler(currentItem_Write);
currentItem.AfterWrite += new Outlook.ItemEvents_10_AfterWriteEventHandler(currentItem_AfterWrite);
}