gpt4 book ai didi

c# - DTE2 事件不触发

转载 作者:可可西里 更新时间:2023-11-01 08:56:52 24 4
gpt4 key购买 nike

在尝试开发我的第一个 VS 插件时,我在触发 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 [...]
}
}

最佳答案

您需要保存 DocumentEvents 类。我认为它们会被处理掉或被垃圾收集起来。

以我为例。

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);
}

关于c# - DTE2 事件不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5405167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com