我使用“Visual Studio 插件”向导创建了一个新的插件项目,现在,我正在尝试添加一些事件处理程序:
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;
}
但无论我做什么,我的处理程序都不会被执行!
我瞎了吗?我是否还需要做任何其他事情来注册这些处理程序,或者为什么它不起作用?
最佳答案
看来你是垃圾收集器的受害者。见: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;
}
关于c# - 永远不会执行加载项事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14165885/