运行VS 2012.我创建了一个加载项。我想处理OnEnterBreakMode事件。调用Exec方法。我试过返回processed = true / false。永远不会调用处理程序。我尝试了几种DTE和DTE2。我转到工具菜单>单击“MyAddIn1”并调用Exec方法。我验证了事件的约束。我不知道加载项的生命周期是如何工作的。
StartEvents
不是覆盖,它与任何东西都没有关联。我觉得很奇怪......
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin1.Connect.MyAddin1")
{
//handled = true;
// Place the following code in the Exec method of the add-in:
EnvDTE.DebuggerEvents debugEvents = _applicationObject.Events.DebuggerEvents;
debugEvents.OnEnterBreakMode += new _dispDebuggerEvents_OnEnterBreakModeEventHandler(Connect.BreakHandler);
return;
}
}
}
private DTE _applicationObject;
private AddIn _addInInstance;
// Place the following Event handler code in the add-in:
// Needed to activate event handlers in Connect.Exec.
public static void StartEvents(DTE dte)
{
Console.WriteLine("Events are attached.");
}
// OnEnterBreakMode Event.
public static void BreakHandler(dbgEventReason reason, ref dbgExecutionAction execAction)
{
Console.WriteLine("Debugger enters break mode. " + "Reason: " + reason.ToString());
}
答案 0 :(得分:2)
如果为事件创建局部变量并订阅它,那么在保留范围后GC将释放该变量,并且不会触发事件处理程序。
如果你使debugEvents
成员变量,那么它应该可以正常工作。