Search code examples
c#wpfvisual-studiovisual-studio-extensions

IWpfTextViewCreationListener.TextViewCreated not triggered


I'm writing my own Visual Studio Extension and I need to get the XPATH from the selected text inside the Visual Studio Editor.

The ActiveDocument is a valid XHTML file where the user can select a random line in the editor and I can do some Magic with the xpath.

The following code is part of the Visual Studio Extension "XPATH Tools" which is doing just what I need. If I build the extension and open a XML file the TextViewCreated gets triggered and everything is working fine. But if I import some parts of the Plugin into my own Plugin it's not getting triggered and I just can't find the reason for that. Everything is identical from my point of view.

My plan is to use the Magic of the existing Extension and use the resulting XPATH String for my plugin.

Constants.XmlContentTypeName is "XML" (also tested "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;
    }

Solution

  • This fixed the Problem. Event is triggered correctly now.