IWpfTextViewCreationListener.TextViewCreated 未触发
IWpfTextViewCreationListener.TextViewCreated not triggered
我正在编写自己的 Visual Studio 扩展,我需要从 Visual Studio 编辑器中 selected 文本中获取 XPATH。
ActiveDocument 是一个有效的 XHTML 文件,用户可以在其中 select 编辑器中的任意一行,我可以用 xpath 做一些魔术。
以下代码是 Visual Studio 扩展“XPATH Tools”的一部分,它正是我需要的。如果我构建扩展并打开 XML 文件,则会触发 TextViewCreated
并且一切正常。但是,如果我将插件的某些部分导入到我自己的插件中,它不会被触发,我就是找不到原因。从我的角度来看,一切都是一样的。
我的计划是使用现有扩展的魔力,并将生成的 XPATH 字符串用于我的插件。
Constants.XmlContentTypeName
是"XML"(也测试过"XHTML")
[Export(typeof(IWpfTextViewCreationListener))]
[TextViewRole(PredefinedTextViewRoles.Document)]
[ContentType(Constants.XmlContentTypeName)]
internal class XmlTextViewCreationListener : IWpfTextViewCreationListener
{
private readonly XmlRepository _repository;
private readonly ActiveDocument _activeDocument;
public XmlTextViewCreationListener()
: this(Registry.Current.Get<XmlRepository>(), Registry.Current.Get<ActiveDocument>())
{
}
public XmlTextViewCreationListener(XmlRepository repository, ActiveDocument activeDocument)
{
_repository = repository;
_activeDocument = activeDocument;
}
public void TextViewCreated(IWpfTextView textView)
{
if(textView?.Caret == null)
{
return;
}
_repository.LoadXml(textView.TextSnapshot.GetText(), _activeDocument.AbsolutePath);
textView.Closed += ResetXml;
textView.Caret.PositionChanged += StoreCurrentNode;
}
解决了问题。现在事件已正确触发。
我正在编写自己的 Visual Studio 扩展,我需要从 Visual Studio 编辑器中 selected 文本中获取 XPATH。
ActiveDocument 是一个有效的 XHTML 文件,用户可以在其中 select 编辑器中的任意一行,我可以用 xpath 做一些魔术。
以下代码是 Visual Studio 扩展“XPATH Tools”的一部分,它正是我需要的。如果我构建扩展并打开 XML 文件,则会触发 TextViewCreated
并且一切正常。但是,如果我将插件的某些部分导入到我自己的插件中,它不会被触发,我就是找不到原因。从我的角度来看,一切都是一样的。
我的计划是使用现有扩展的魔力,并将生成的 XPATH 字符串用于我的插件。
Constants.XmlContentTypeName
是"XML"(也测试过"XHTML")
[Export(typeof(IWpfTextViewCreationListener))]
[TextViewRole(PredefinedTextViewRoles.Document)]
[ContentType(Constants.XmlContentTypeName)]
internal class XmlTextViewCreationListener : IWpfTextViewCreationListener
{
private readonly XmlRepository _repository;
private readonly ActiveDocument _activeDocument;
public XmlTextViewCreationListener()
: this(Registry.Current.Get<XmlRepository>(), Registry.Current.Get<ActiveDocument>())
{
}
public XmlTextViewCreationListener(XmlRepository repository, ActiveDocument activeDocument)
{
_repository = repository;
_activeDocument = activeDocument;
}
public void TextViewCreated(IWpfTextView textView)
{
if(textView?.Caret == null)
{
return;
}
_repository.LoadXml(textView.TextSnapshot.GetText(), _activeDocument.AbsolutePath);
textView.Closed += ResetXml;
textView.Caret.PositionChanged += StoreCurrentNode;
}