c# - Outlook VSTO 未在 NewMailEx 事件上触发?

标签 c# .net-3.5 outlook vsto

我用 C# 创建了一个 VSTO,它应该 Hook Outlook 2007 的 NewMailEx 事件。但是,当我手动发送/接收或收件箱中只有 1 封未读邮件时,它有时不会触发。看起来好像它在消息实际到达之前就在收件箱中触发了。

除了使用 VSTO 的 ItemAdd 或 NewMailEX 之外,是否还有更好的方法每次监控新消息?

最佳答案

原因是:“GC 收集 .NET 对象,该对象包装来自 Outlook 的 COM 对象)”。 解决方案是保留对此 .NET 对象的引用。最简单的方法是:

// this is helper collection.
// there are all wrapper objects
// , which should not be collected by GC
private List<object> holdedObjects = new List<object>();

// hooks necesary events
void HookEvents() {
    // finds button in commandbars
    CommandBarButton btnSomeButton = FindCommandBarButton( "MyButton ");
    // hooks "Click" event
    btnSomeButton.Click += btnSomeButton_Click;
    // add "btnSomeButton" object to collection and
    // and prevent themfrom collecting by GC
    holdedObjects.Add( btnSomeButton );
}

如果需要,您还可以为此(和其他)具体按钮(或其他对象)设置一个特殊字段。但这是最常见的解决方案。

关于c# - Outlook VSTO 未在 NewMailEx 事件上触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/202212/

相关文章:

performance - 为什么 Outlook 关闭时 Excel VBA 的运行速度明显加快?

c# - 办公室功能区是否可以有多个自定义选项卡?

c# - 如何在 .net 3.5 中获得 Task<T> 的等价物?

c# - 无法从类转换为通用接口(interface)

c# - 对从 Linq2Sql 的 var 中获取 int 感到困惑

c# - TcpClient 发送/关闭问题

c# - 同一类上的两组序列化属性

c# - List<MyClass> 作为 DropDownList 的数据源?

sql - 为 Outlook 2007 构建表单

c# - 单击按钮时隐藏弹出按钮 (DelegateCommand)