加载项事件永远不会执行 [英] Add-In events are never executed

查看:197
本文介绍了加载项事件永远不会执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio加载项向导创建一个新的Addin项目,现在我正在尝试添加一些事件处理程序:

  public void OnConnection(object application,ext_ConnectMode connectMode,object addInInst,ref Array custom)
{
_applicationObject =(DTE2)
_addInInstance =(AddIn)addInInst;

_applicationObject.Events.BuildEvents.OnBuildBegin + = BuildEvents_OnBuildBegin;
_applicationObject.Events.BuildEvents.OnBuildDone + = BuildEvents_OnBuildDone;
_applicationObject.Events.SelectionEvents.OnChange + = SelectionEvents_OnChange;
_applicationObject.Events.DocumentEvents.DocumentOpened + = DocumentEvents_DocumentOpened;
_applicationObject.Events.DocumentEvents.DocumentSaved + = DocumentEvents_DocumentSaved;
}

但无论我做什么,我的处理程序都永远不会执行!

$我是盲人吗?我必须做任何事情来注册这些处理程序,或者为什么不工作?

解决方案

看起来你是一个受害者的垃圾收集器。请参阅: http://www.mztools.com/articles/2005/mz2005012.aspx

  private readonly BuildEvents _buildEvents; 
private readonly SelectionEvents _selectionEvents;
private readonly DocumentEvents _documentEvents;
私人只读事件_events;

public void OnConnection(object application,ext_ConnectMode connectMode,object addInInst,ref Array custom)
{
_applicationObject =(DTE2)
_addInInstance =(AddIn)addInInst;
_events = _applicationObject.Events;

_buildEvents = _events.BuildEvents;
_buildEvents.OnBuildBegin + = BuildEvents_OnBuildBegin;
_buildEvents.OnBuildDone + = BuildEvents_OnBuildDone;

_selectionEvents = _events.SelectionEvents;
_selectionEvents.OnChange + = SelectionEvents_OnChange;

_documentEvents = _events.DocumentEvents;
_documentEvents.DocumentOpened + = DocumentEvents_DocumentOpened;
_documentEvents.DocumentSaved + = DocumentEvents_DocumentSaved;
}


I used the "Add-In for Visual Studio" wizard to create a new Addin project and now, I'm trying to add some event handlers:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    _applicationObject.Events.BuildEvents.OnBuildBegin += BuildEvents_OnBuildBegin;
    _applicationObject.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone;
    _applicationObject.Events.SelectionEvents.OnChange += SelectionEvents_OnChange;
    _applicationObject.Events.DocumentEvents.DocumentOpened += DocumentEvents_DocumentOpened;
    _applicationObject.Events.DocumentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
}

But whatever I do, my handlers are never executed!

Am I blind? Do I have to do anything else to register these handlers or why doesn't it work?

解决方案

Seems you're a victim of the Garbage Collector. See: http://www.mztools.com/articles/2005/mz2005012.aspx

 private readonly BuildEvents _buildEvents;
 private readonly SelectionEvents _selectionEvents;
 private readonly DocumentEvents _documentEvents;
 private readonly Events _events;

 public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
 {
     _applicationObject = (DTE2)application;
     _addInInstance = (AddIn)addInInst;
     _events = _applicationObject.Events;

     _buildEvents = _events.BuildEvents;
     _buildEvents.OnBuildBegin += BuildEvents_OnBuildBegin;
     _buildEvents.OnBuildDone += BuildEvents_OnBuildDone;

     _selectionEvents = _events.SelectionEvents;
     _selectionEvents.OnChange += SelectionEvents_OnChange;

     _documentEvents = _events.DocumentEvents;
     _documentEvents.DocumentOpened += DocumentEvents_DocumentOpened;
     _documentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
 }

这篇关于加载项事件永远不会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆