展望2013& Outlook加载项:: MAPIFolderEvents_12_Event.BeforeItemMove不会被触发?

时间:2015-09-06 01:27:52

标签: c# outlook outlook-addin

我正在努力实现阿德里安·布朗的非常好Outlook Add-In code,它有3次中有2次出现。 ItemAddItemChange事件按预期触发,但MAPIFolderEvents_12_Event.BeforeItemMove的事件处理程序似乎没有执行任何操作 - 我甚至没有点击断点在事件处理程序的第一行。

更多清晰度代码

这是CalendarMonitor类;它监视文件夹的Items集合上的ItemAdd,ItemChange事件,以及MAPIFolder上的BeforeItemMove:

public class CalendarMonitor
{
    private Explorer _explorer;
    private List<string> _folderPaths;
    private List<MAPIFolder> _calendarFolders;
    private List<Items> _calendarItems;
    private MAPIFolder _deletedItemsFolder;

    public event EventHandler<EventArgs<AppointmentItem>> AppointmentAdded;
    public event EventHandler<EventArgs<AppointmentItem>> AppointmentModified;
    public event EventHandler<CancelEventArgs<AppointmentItem>> AppointmentDeleting;

    public CalendarMonitor(Explorer explorer)
    {
        _folderPaths = new List<string>();
        _calendarFolders = new List<MAPIFolder>();
        _calendarItems = new List<Items>();

        _explorer = explorer;
        _explorer.BeforeFolderSwitch += Explorer_BeforeFolderSwitch;

        var session = _explorer.Session;
        try
        {
            _deletedItemsFolder = session.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);
            HookupDefaultCalendarEvents(session);
        }
        finally
        {
            Marshal.ReleaseComObject(session);
            session = null;
        }
    }

    private void HookupDefaultCalendarEvents(_NameSpace session)
    {
        var folder = session.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
        if (folder == null) return;

        try
        {
            HookupCalendarEvents(folder);
        }
        finally
        {
            Marshal.ReleaseComObject(folder);
            folder = null;
        }
    }

    private void Explorer_BeforeFolderSwitch(object obj, ref bool cancel)
    {
        var folder = (obj as MAPIFolder);
        if (folder == null) return;

        try
        {
            // Hookup events to any other Calendar folder opened.
            if (folder.DefaultItemType == OlItemType.olAppointmentItem)
                HookupCalendarEvents(folder);
        }
        finally
        {
            Marshal.ReleaseComObject(folder);
            folder = null;
        }
    }

    private void HookupCalendarEvents(MAPIFolder calendarFolder)
    {
        if (calendarFolder.DefaultItemType != OlItemType.olAppointmentItem)
        {
            throw new ArgumentException("The MAPIFolder must use AppointmentItems as the default type.");
        }

        // Ignore other user's calendars.
        if (_folderPaths.Contains(calendarFolder.FolderPath) || (!IsUsersCalendar(calendarFolder))) return;

        var items = calendarFolder.Items;

        // Store folder path to prevent repeating listeners
        _folderPaths.Add(calendarFolder.FolderPath);

        // Store a reference to the folder & items to prevent garbage collection
        _calendarFolders.Add(calendarFolder);
        _calendarItems.Add(items);

        // Add listeners
        ((MAPIFolderEvents_12_Event)calendarFolder).BeforeItemMove += Calendar_BeforeItemMove;
        items.ItemChange += CalendarItems_ItemChange;
        items.ItemAdd += CalendarItems_ItemAdd;
    }

    private void CalendarItems_ItemAdd(object obj)
    {
        var appointment = (obj as AppointmentItem);
        if (appointment == null) return;

        try
        {
            if (AppointmentAdded != null)
                AppointmentAdded(this, new EventArgs<AppointmentItem>(appointment));
        }
        finally
        {
            Marshal.ReleaseComObject(appointment);
            appointment = null;
        }
    }

    private void CalendarItems_ItemChange(object obj)
    {
        var appointment = (obj as AppointmentItem);
        if (appointment == null) return;

        try
        {
            if (AppointmentModified != null)
                AppointmentModified(this, new EventArgs<AppointmentItem>(appointment));
        }
        finally
        {
            Marshal.ReleaseComObject(appointment);
            appointment = null;
        }
    }

    private void Calendar_BeforeItemMove(object obj, MAPIFolder moveToFolder, ref bool cancel)
    { 
        if ((moveToFolder != null) && (!IsDeletedItemsFolder(moveToFolder))) return;

        var appointment = (obj as AppointmentItem);
        if (appointment == null) return;

        try
        {
            if (AppointmentDeleting == null) return;

            // Listeners to the AppointmentDeleting event can cancel the move operation if moving
            // to the deleted items folder.
            var args = new CancelEventArgs<AppointmentItem>(appointment);
            AppointmentDeleting(this, args);
            cancel = args.Cancel;
        }
        finally
        {
            Marshal.ReleaseComObject(appointment);
            appointment = null;
        }
    }

    private bool IsUsersCalendar(MAPIFolder folder)
    {
        // This is based purely on my observations so far - a better way?
        return (folder.Store != null);
    }

    private bool IsDeletedItemsFolder(MAPIFolder folder)
    {
        return (folder.EntryID == _deletedItemsFolder.EntryID);
    }

    public AppointmentItem Item { get; set; }
}

新信息:

我已经完成了一些额外的“故障排除”并提供了更多信息:一时兴起,我在Outlook中创建了一个新日历(在调试时)并且看到BeforeItemMove事件就像我期望的那样触发删除新日历中的约会时,但它仍然无法在原始日历中使用。

如果我退出调试会话并重新启动,那么日历的事件都不会按预期运行,尽管之前工作正常。任何新日历的BeforeItemMove事件都可以正常工作,直到我关闭Outlook - 然后它又回到没有响应。

我希望这些额外的信息能够为那些比我更聪明的人提供见解。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

必须在全局/类级别声明calFolder变量,以避免被垃圾收集器释放。

答案 1 :(得分:0)

您在哪里声明了源对象?当事件被解雇时它是否存在?

无论如何,您可以考虑开发一个检查器包装器。有关详细信息,请参阅Developing an Inspector Wrapper for Outlook 2010How to: Implement a Wrapper for Inspectors and Track Item-Level Events in Each Inspector

C#和VB.NET中的示例项目也可用 - Outlook 2010: Developing an Inspector Wrapper