2

我想将 EventHandlers 用于约会和联系人(以及稍后的任务)。

我有一个执行以下代码的 ContactProgram 类:

Outlook.Items myContactItems = myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Items; 
myContactItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(myContactItems_Add);
myContactItems.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(myContactItems_Change);
myContactItems.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(myContactItems_Remove);

我有一个执行以下代码的 AppointmentPogram 类:

Outlook.Items myAppointmentItems = myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items;
myAppointmentItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(myAppointmentItems_Add);
myAppointmentItems.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(myAppointmentItems_Change);
myAppointmentItems.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(myAppointmentItems_Remove);

如果我对两者都使用 EventHandler,那么 Appointment-EventHandler 将不会触发。但是,如果我注释掉创建 Contact-EventHandlers 的代码,那么 Appointment-EventHandlers 就会被触发。(在我的程序中,首先创建联系人事件处理程序,然后创建约会事件处理程序。)

我只能创建一次 EventHandler,然后检查作为参数给出的对象的类型。但不幸的是,联系人事件处理程序需要指向 olFolderContacts 和约会事件处理程序 olFolderCalendar。

有什么建议么?

编辑:我的新代码:

...
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
public partial class ThisAddIn
{
    private static Outlook.Application myApplication = new Outlook.Application();
    private List<OutlookContact> allContacts = new List<OutlookContact>();
    private Outlook.Folder myContactsFolder = (Outlook.Folder)myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
    private ContactProgram contactProgram;

    private List<OutlookAppointment> allAppointments = new List<OutlookAppointment>();
    private Outlook.Folder myAppointmentsFolder = (Outlook.Folder)myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    private AppointmentProgram appointmentProgram;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        contactProgram = new ContactProgram(myApplication, allContacts, myContactsFolder);
        contactProgram.Start();
        appointmentProgram = new AppointmentProgram(myApplication, allAppointments, myAppointmentsFolder);
        appointmentProgram.Start();

        //initialise EventHandlers
        //myContactItems = myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Items;//= myContactsFolder.Items;
        Outlook.Items myContactItems = myContactsFolder.Items;
        myContactItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(myContactItems_Add);
        myContactItems.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(myContactItems_Change);
        myContactItems.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(myContactItems_Remove);

        //myAppointmentItems = myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items;
        Outlook.Items myAppointmentItems = myAppointmentsFolder.Items;
        myAppointmentItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(myAppointmentItems_Add);
        myAppointmentItems.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(myAppointmentItems_Change);
        myAppointmentItems.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(myAppointmentItems_Remove);
    }

    private static void myContactItems_Add(object item)
    {
        ContactProgram.myContactItems_Add(item);
    }
    ...

}
4

1 回答 1

4

您寻求的事件处理程序 ( ItemAdd, ItemChange, ItemRemove) 在Folder关卡中完成。由于日历和联系人是不同的文件夹 - 您不能通过一次注册来监听两者的更改事件 - 它需要为每个Folder商店提供单独的处理程序。

如果您的事件未触发,则可能是由于您没有保留变量myAppointmentItemsmyContactItems作为类级别变量的范围问题。请参阅相关的 SO 帖子

于 2012-03-21T13:17:44.223 回答