6

I'd like to get notified whenever the caret position changed in the active text view. The only thing EnvDTE seems to offer is the LineChanged event, which of-course does not get raised when moving the caret left or right within the same line.

I realize VS2010's Editor Extensibility lets you do this with no sweat, but I need a solution that is backwards compatible with VS2008.

4

2 回答 2

1

你看到了吗:DTE2 事件不会触发

您必须保留 Events 对象的本地实例,否则事件不会触发(我假设是因为 COM 支持的 Events 对象超出范围并被 GC):

public class MyVSPackage
{ 
   TextEditorEvents _textEditorEvents;

   public MyVSPackage()
   {
        _textEditorEvents = DTE.Events.TextEditorEvents;

        _textEditorEvents.LineChanged += (point, endPoint, hint) => //Do something here
   }
}
于 2013-05-23T13:38:29.767 回答
0

我找到了解决方案。解决方案是创建一个 IOleCommandTarget 并在 IVsTextView 上注册它(请参阅此博客文章中的最后两位代码(在 Herbrew 中))。然后,每次发出命令时,我都会检查插入符号的位置是否发生了变化。另请参阅:此博客文章 - 如何在 Visual Studio 文本编辑器中拦截按键

于 2013-05-24T09:52:48.300 回答