c# – 从不执行加载项事件

前端之家收集整理的这篇文章主要介绍了c# – 从不执行加载项事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用“Visual Studio的加载项”向导创建一个新的Addin项目,现在我正在尝试添加一些事件处理程序:
  1. public void OnConnection(object application,ext_ConnectMode connectMode,object addInInst,ref Array custom)
  2. {
  3. _applicationObject = (DTE2)application;
  4. _addInInstance = (AddIn)addInInst;
  5.  
  6. _applicationObject.Events.BuildEvents.OnBuildBegin += BuildEvents_OnBuildBegin;
  7. _applicationObject.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone;
  8. _applicationObject.Events.SelectionEvents.OnChange += SelectionEvents_OnChange;
  9. _applicationObject.Events.DocumentEvents.DocumentOpened += DocumentEvents_DocumentOpened;
  10. _applicationObject.Events.DocumentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
  11. }

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

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

解决方法

看来你是垃圾收集者的受害者.见: http://www.mztools.com/articles/2005/mz2005012.aspx
  1. private readonly BuildEvents _buildEvents;
  2. private readonly SelectionEvents _selectionEvents;
  3. private readonly DocumentEvents _documentEvents;
  4. private readonly Events _events;
  5.  
  6. public void OnConnection(object application,ref Array custom)
  7. {
  8. _applicationObject = (DTE2)application;
  9. _addInInstance = (AddIn)addInInst;
  10. _events = _applicationObject.Events;
  11.  
  12. _buildEvents = _events.BuildEvents;
  13. _buildEvents.OnBuildBegin += BuildEvents_OnBuildBegin;
  14. _buildEvents.OnBuildDone += BuildEvents_OnBuildDone;
  15.  
  16. _selectionEvents = _events.SelectionEvents;
  17. _selectionEvents.OnChange += SelectionEvents_OnChange;
  18.  
  19. _documentEvents = _events.DocumentEvents;
  20. _documentEvents.DocumentOpened += DocumentEvents_DocumentOpened;
  21. _documentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
  22. }

猜你在找的C#相关文章