首页 > 解决方案 > 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;
    }

标签: c#wpfvisual-studiovisual-studio-extensions

解决方案


解决了问题。事件现在正确触发。


推荐阅读