简体   繁体   English

DTE2 事件不会触发

[英]DTE2 events don't fire

While trying to develop my first VS Addin, I am having issues in firing DTE2 events.在尝试开发我的第一个 VS Addin 时,我在触发 DTE2 事件时遇到了问题。

Basically, the DocumentOpened and LineChanged events don't fire for some reason.基本上, DocumentOpened 和 LineChanged 事件由于某种原因不会触发。 What important part did I miss?我错过了什么重要的部分?

namespace TestAddin {
  public class Connect : IDTExtensibility2 {
    private AddIn _addInInstance;
    private DTE2 _applicationObject;

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

      _applicationObject.Events.DocumentEvents.DocumentOpened += InitializeFoldingOnDocument;
      _applicationObject.Events.TextEditorEvents.LineChanged += UpdateFoldingOnDocument;
    }

    private void UpdateFoldingOnDocument(TextPoint startpoint, TextPoint endpoint, int hint) {
      RegionFolding(_applicationObject.ActiveDocument);
    }

    private void InitializeFoldingOnDocument(Document document) {
      RegionFolding(document);
    }

    private void RegionFolding(Document _document) {
      // Do the folding [...]
    }

    // Other IDTExtensibility2 Members [...]
  }
}

You need to save the DocumentEvents class.您需要保存DocumentEvents类。 I think they will be disposed or garbage collected else.我认为它们将被处理或垃圾收集。

In my case.就我而言。

private SolutionEvents solutionEvents;

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    Globals.DTE = (DTE2)application;
    Globals.Addin = (AddIn)addInInst;

    solutionEvents = Globals.DTE.Events.SolutionEvents;
    solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(SolutionEvents_Opened);
    solutionEvents.BeforeClosing += new _dispSolutionEvents_BeforeClosingEventHandler(SolutionEvents_BeforeClosing);
}

I found a different solution to this problem.我找到了这个问题的不同解决方案。

I was boxing and unboxing my DTE object before doing my event subscriptions.在进行事件订阅之前,我正在装箱和拆箱我的 DTE 对象。 This ulitmately proved the culprit for me.这最终证明了我的罪魁祸首。 While this wasn't your issue, it could help others who have similar issues;虽然这不是您的问题,但它可以帮助其他有类似问题的人; and is good to know so that you don't make the same mistakes I did which took an extreme amount of time to resolve.并且很高兴知道这样你就不会犯我犯过的同样的错误,我花了很多时间来解决。

See here: http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/eb1e8fd1-32ad-498c-98e9-25ee3da71004请参阅此处: http : //social.msdn.microsoft.com/Forums/en-US/vsx/thread/eb1e8fd1-32ad-498c-98e9-25ee3da71004

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM