c# - IWpfTextViewCreationListener.TextViewCreated 未触发
问题描述
我正在编写自己的 Visual Studio 扩展,我需要从 Visual Studio 编辑器中的选定文本中获取 XPATH。
ActiveDocument 是一个有效的 XHTML 文件,用户可以在编辑器中选择一个随机行,我可以用 xpath 做一些魔术。
以下代码是 Visual Studio 扩展“ XPATH 工具”的一部分,它正在做我需要的事情。如果我构建扩展并打开一个 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;
}
解决方案
这解决了问题。事件现在正确触发。
推荐阅读
- python - sklearn 中的 RandomForestRegressor 给出负分
- javascript - 用户可以在 svg 元素上画一条线
- php - fwrite 在 Web 服务器上返回 int(0) 但不在 Wamp 上
- mongodb - 如何使用 3 个集合和 $loop 转换数据
- python - 为什么张量流中的for循环这么慢
- python - 如何更改 pyqt5 QTableWidget 中选定或单击的单元格的背景颜色?
- c - 如何停止或中断获取键盘输入缓冲区其余部分的低级读取功能
- java - 如何使用 DateTimeFormatter 本地化数字?
- asp.net-core - NetCore InputTagHelper 使用反射
- c# - 如果一个单词只包含几个字符,我应该使用哪种正则表达式