Outlook 2016 加载项未触发 ItemAdd 事件

发布于 2025-01-19 00:08:00 字数 1275 浏览 4 评论 0原文

我正在开发一个旧的 Outlook 加载项,该加载项似乎不会在 ItemAdd 事件上触发

这是现在的代码:

public partial class MyAddin
{
    private MAPIFolder sentItemsFolder;

    private void InternalStartup()
    {
        this.Startup += this.MyAddinStartup;
        this.Shutdown += this.MyAddinShutdown;
    }

    private void InboxFolderItemAdded(object item)
    {
        // Code that doesn't get executed
    }

    private void MyAddinShutdown(object sender, EventArgs e)
    {
        this.sentItemsFolder.Items.ItemAdd -= this.InboxFolderItemAdded;
        this.sentItemsFolder.ReleaseComObject();
        this.sentItemsFolder = null;
    }

    private void MyAddinStartup(object sender, EventArgs e)
    {
        using (var ns = this.Application.GetNamespace("MAPI").WithComCleanup())
        {
            this.sentItemsFolder = ns.Resource.GetDefaultFolder(OlDefaultFolder.oldFolderSentMail);
            this.sentItemsFolder.Items.ItemAdd += this.InboxFolderItemAdded;
        }
    }
}

InboxFolderItemAdded() 中放置断点时,永远不会到达断点。我已经足够说明,似乎与另一个加载项存在某种不兼容,因为当禁用该加载项时代码可以工作。现在的问题是,我不知道足以解决可能出现的问题,是否可以以任何方式将 SentItemsFolder 对象标记为共享?以防其他加载项尝试获取独占访问权限(真的不知道这是如何工作的)。我没有破坏代码的加载项的源代码。

我想真正的问题是:如何调试与其他加载项的不兼容问题,或者是否有某种方法可以编辑我必须绕过它的代码?

I'm working on an old Outlook add-in that doesn't seem to trigger on the ItemAdd event

This is the code right now:

public partial class MyAddin
{
    private MAPIFolder sentItemsFolder;

    private void InternalStartup()
    {
        this.Startup += this.MyAddinStartup;
        this.Shutdown += this.MyAddinShutdown;
    }

    private void InboxFolderItemAdded(object item)
    {
        // Code that doesn't get executed
    }

    private void MyAddinShutdown(object sender, EventArgs e)
    {
        this.sentItemsFolder.Items.ItemAdd -= this.InboxFolderItemAdded;
        this.sentItemsFolder.ReleaseComObject();
        this.sentItemsFolder = null;
    }

    private void MyAddinStartup(object sender, EventArgs e)
    {
        using (var ns = this.Application.GetNamespace("MAPI").WithComCleanup())
        {
            this.sentItemsFolder = ns.Resource.GetDefaultFolder(OlDefaultFolder.oldFolderSentMail);
            this.sentItemsFolder.Items.ItemAdd += this.InboxFolderItemAdded;
        }
    }
}

When placing a breakpoint in InboxFolderItemAdded() the breakpoint is never reached. I've come far enough to state that there seems to be some kind of incompatibility with another add-in since the code works when that add-in is disabled. The problem right now is that i don't know enough to troubleshoot what might be the issue, is is possible to flag the SentItemsFolder object as shared in any way? In case the other add-in is trying to take exclusive access (don't really know how this works). I do not have the source code for the add-in that's breaking the code.

I guess the real question is: How do i debug incompatibility issues with other add-ins, or is there some way i can edit the code i have to get around it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2025-01-26 00:08:00
this.sentItemsFolder.Items.ItemAdd -= this.InboxFolderItemAdded;

在类级别上定义源对象:

Outlook.Items items = null;

然后,在方法中,您可以将其设置并订阅事件:

items = this.sentItemsFolder.Items;
items.ItemAdd -= this.InboxFolderItemAdded;

因此,基本上,您需要在类级别声明源item在类级别防止它被GC刷卡。

同样适用于您要订阅项目事件的循环:

    private void MyAddinStartup(object sender, EventArgs e)
    {
        using (var ns = this.Application.GetNamespace("MAPI").WithComCleanup())
        {
            this.sentItemsFolder = ns.Resource.GetDefaultFolder(OlDefaultFolder.oldFolderSentMail);
            this.sentItemsFolder.Items.ItemAdd += this.InboxFolderItemAdded;
        }
    }

您可以定义Outlook.items对象的列表,您可以在其中保留源对象:

List<Outlook.Items> items = new List<Outlook.Items>();

private void MyAddinStartup(object sender, EventArgs e)
{
    using (var ns = this.Application.GetNamespace("MAPI").WithComCleanup())
    {
        this.sentItemsFolder = ns.Resource.GetDefaultFolder(OlDefaultFolder.oldFolderSentMail);
        Outlook.Items itemsObject = this.sentItemsFolder.Items;        
        itemsObject.ItemAdd += this.InboxFolderItemAdded;
        // add to the list to prevent GC from swiping it out
        items.Add(itemsObject);
    }
}
this.sentItemsFolder.Items.ItemAdd -= this.InboxFolderItemAdded;

Define the source object at the class level:

Outlook.Items items = null;

Then in the method you could set it up and subscribe to the event(s):

items = this.sentItemsFolder.Items;
items.ItemAdd -= this.InboxFolderItemAdded;

So, basically you need to declare the source Items object at the class level to prevent it from being swiped by the GC.

The same applies to the loop where you are subscribing to the Items events:

    private void MyAddinStartup(object sender, EventArgs e)
    {
        using (var ns = this.Application.GetNamespace("MAPI").WithComCleanup())
        {
            this.sentItemsFolder = ns.Resource.GetDefaultFolder(OlDefaultFolder.oldFolderSentMail);
            this.sentItemsFolder.Items.ItemAdd += this.InboxFolderItemAdded;
        }
    }

You may define a list of Outlook.Items objects where you could keep the source objects:

List<Outlook.Items> items = new List<Outlook.Items>();

private void MyAddinStartup(object sender, EventArgs e)
{
    using (var ns = this.Application.GetNamespace("MAPI").WithComCleanup())
    {
        this.sentItemsFolder = ns.Resource.GetDefaultFolder(OlDefaultFolder.oldFolderSentMail);
        Outlook.Items itemsObject = this.sentItemsFolder.Items;        
        itemsObject.ItemAdd += this.InboxFolderItemAdded;
        // add to the list to prevent GC from swiping it out
        items.Add(itemsObject);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文