我正在编写一个 Outlook 加载项,并想在保存后(何时)对约会数据做一些事情(此处不相关)。
(我是 Outlook-Addins 的新手)
所以我发现有一个 AfterWrite 事件,我可以在其中注册一个方法。
Application 上有一个 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 注册一个获取对象的函数,该对象可以转换为约会。
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;
}
}
但是事件没有被触发! 我现在很沮丧
最佳答案
好的,我想我明白了。
您必须在包含您的约会的文件夹上注册处理程序。
我自己阅读并体验到,保存对您注册事件的对象的引用至关重要,所以我这样做了。
这就是代码
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 仍然没有让我得到被移除的对象。
关于c# - 保存 Outlook 约会后的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11610535/