我正在编写一个Outlook加载项,并希望在保存之后(何时)使用约会的数据执行某些操作(此处不相关)。
(我是Outlook-Addins的新手)
所以我发现有一个 AfterWrite 事件,我可以注册一个方法。
应用程序上有 ItemLoad 事件。
所以我的第一个Efford是这样的:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// ...
this.Application.ItemLoad +=
new Outlook.ApplicationEvents_11_ItemLoadEventHandler(atItemLoad);
}
public void atItemLoad(Object item)
{
Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
if (aitem != null)
{
aitem.AfterWrite +=
new Outlook.ItemEvents_10_AfterWriteEventHandler(afterWrite);
}
}
public void afterWrite()
{
// Who was written?
MessageBox.Show("it was written!");
}
问题是,我不知道如何获取约会的数据,这引发了事件。
Application.ItemLoad 注册一个获取Object的函数,该函数可以强制转换为 Appointment 。
AfterWrite 没有。我想要这样的事情:
public void afterWrite(Outlook.AppointmentItem aitem)
{
// do something with the data from the Appointment
MessageBox.Show(aitem.Subject + " was written!");
}
我担心我正朝着完全错误的方向进行研究。
*对不起,如果我的英语很乱 - 那不是我的母语
编辑:
我甚至试过这样的结构:private List<AppointmentEventHolder> holderList = new List<AppointmentEventHolder>();
internal class AppointmentEventHolder
{
private Outlook.AppointmentItem aitem = null;
public AppointmentEventHolder(Outlook.AppointmentItem item)
{
aitem = item;
}
public void onWrite()
{
MessageBox.Show("write: " + aitem.Subject);
}
}
public void atItemLoad(Object item)
{
Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
if (aitem != null)
{
AppointmentEventHolder aHolder = new AppointmentEventHolder(aitem);
holderList.Add(aHolder);
aitem.AfterWrite += aHolder.onWrite;
}
}
但事件不会被解雇! 我现在很沮丧
答案 0 :(得分:6)
我自己阅读和体验,保存对您注册事件的对象的引用至关重要,所以我这样做了。
这就是代码
private Outlook.MAPIFolder _CalendarFolder = null;
private Outlook.Items _CalendarItems = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.MAPIFolder calendarFolder =
this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
// get the Termine24-Folder (if not found, create it)
try
{
_CalendarFolder = calendarFolder.Folders[Constants.FOLDERNAME];
}
catch
{
_CalendarFolder = calendarFolder.Folders.Add(Constants.FOLDERNAME);
}
_CalendarItems = _CalendarFolder.Items;
attachEvents();
}
public void attachEvents()
{
_CalendarItems.ItemAdd += Item_Add;
_CalendarItems.ItemChange += Item_Change;
_CalendarItems.ItemRemove += Item_Remove;
}
public void Item_Add(Object item)
{
Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
if (aitem != null)
{
MessageBox.Show("add: " + aitem.Subject);
}
else
{
MessageBox.Show("add, but no appointment");
}
}
public void Item_Change(Object item)
{
Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
if (aitem != null)
{
MessageBox.Show("changed: " + aitem.Subject);
}
else
{
MessageBox.Show("change, but no appointment");
}
}
public void Item_Remove()
{
MessageBox.Show("Item_remove");
}
好的方面是,如果约会在概述中被拖放,我现在也会得到一个事件。
但是Item_Remove仍然没有得到被删除的对象。