Vsto 在没有Visual Studio的情况下未触发MailItem事件
我想处理Outlook邮件项目中的BeforeAttachmentAdd事件。但我的代码在VisualStudio环境中工作,但不在外部工作。你有主意吗 这是我的代码:Vsto 在没有Visual Studio的情况下未触发MailItem事件,vsto,outlook-addin,Vsto,Outlook Addin,我想处理Outlook邮件项目中的BeforeAttachmentAdd事件。但我的代码在VisualStudio环境中工作,但不在外部工作。你有主意吗 这是我的代码: namespace MyOutlookProject { using Microsoft.Office.Interop.Outlook; using OutlookApplication = Microsoft.Office.Interop.Outlook.Application; using OutlookA
namespace MyOutlookProject
{
using Microsoft.Office.Interop.Outlook;
using OutlookApplication = Microsoft.Office.Interop.Outlook.Application;
using OutlookAttachment = Microsoft.Office.Interop.Outlook.Attachment;
using OutlookInspector = Microsoft.Office.Interop.Outlook.Inspector;
using OutlookMail = Microsoft.Office.Interop.Outlook.MailItem;
class MailManager
{
public void StartUp(OutlookApplication application)
{
_inspectors = application.Inspectors;
_inspectors.NewInspector += Inspectors_NewInspector;
}
private void Inspectors_NewInspector(OutlookInspector Inspector)
{
if (Inspector.CurrentItem is OutlookMail)
{
OutlookMail mail = (Inspector.CurrentItem as OutlookMail);
mail.BeforeAttachmentAdd += Mail_BeforeAttachmentAdd;
}
}
private void Mail_BeforeAttachmentAdd(OutlookAttachment Attachment, ref bool Cancel)
{
/*Never called without Visual Studio*/
}
}
}
谢谢您的帮助。据我所知,您可能遇到了“我的按钮停止工作”问题 来自E.卡特和E.利珀特的书 它说 在.NET中开始对Office事件编程时经常遇到的一个问题是“我的按钮停止工作”问题。开发人员将编写一些代码来处理Office工具栏对象模型中CommandBarButton引发的单击事件。此代码有时会暂时工作,但随后会停止。用户将单击按钮,但单击事件似乎已停止工作。此问题的原因是将事件处理程序连接到其生存期与所需事件生存期不匹配的对象。当要连接事件处理程序的对象超出范围或设置为null以便垃圾回收时,会发生这种情况
我认为在你的例子中,
OulookMail
类型的.NET使用变量mail
操纵,这就是罪魁祸首。它的寿命没有得到适当的处理。在VisualStudio中不会发生这种情况的事实是,您可能处于调试模式,该模式会稍微更改垃圾收集,以便在进行测试时不会销毁对象 触发事件的对象(代码中的邮件变量)必须位于全局/类级别,以防止被垃圾收集。在您的情况下,该变量是局部变量
一般来说,您可以打开多个检查器,因此有一个保存对检查器及其邮件项的引用的包装器对象,并在您的加载项中有一个这样的包装器列表可能是有意义的 可能是垃圾收集器