9

在尝试开发我的第一个VS Addin时,我在解决DTE2事件时遇到了问题。DTE2事件不会触发

基本上,由于某种原因,DocumentOpened和LineChanged事件不会触发。我错过了什么重要的部分?

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 [...] 
    } 
} 

回答

20

您需要保存DocumentEvents类。 我认为他们会被废弃或收集其他垃圾。

在我的情况。

+0

就是这样,谢谢! – fjdumont 2011-03-23 12:36:59

+1

作为一名新手VSX开发者,这一款让我非常满意。如果任何人感兴趣的话,请点击这里Microsoft KB问题文章:http://support.microsoft.com/kb/555430 – 2011-07-18 07:56:00

+0

我不知道你是怎么想出来的,但这太棒了! – 2011-09-10 17:21:20

0

我发现了一个不同的解决方案来解决这个问题。

在做我的活动订阅之前,我正在装箱和拆箱我的DTE对象。这有力地证明了我的罪魁祸首。虽然这不是你的问题,但它可以帮助其他有类似问题的人;并且很高兴知道,这样你就不会犯同样的错误,而这些错误需要花费很长时间才能解决。

请看这里:http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/eb1e8fd1-32ad-498c-98e9-25ee3da71004