C# IWpfTextViewCreationListener.TextViewCreated未触发
我正在编写自己的VisualStudio扩展,需要从VisualStudio编辑器中选定的文本中获取XPATH ActiveDocument是一个有效的XHTML文件,用户可以在编辑器中随机选择一行,我可以用xpath做一些魔术 以下代码是Visual Studio扩展“”的一部分,它正执行我需要的操作。如果我构建扩展并打开一个XML文件,C# IWpfTextViewCreationListener.TextViewCreated未触发,c#,wpf,visual-studio,visual-studio-extensions,C#,Wpf,Visual Studio,Visual Studio Extensions,我正在编写自己的VisualStudio扩展,需要从VisualStudio编辑器中选定的文本中获取XPATH ActiveDocument是一个有效的XHTML文件,用户可以在编辑器中随机选择一行,我可以用xpath做一些魔术 以下代码是Visual Studio扩展“”的一部分,它正执行我需要的操作。如果我构建扩展并打开一个XML文件,TextViewCreated就会被触发,一切正常。但是如果我将插件的某些部分导入到我自己的插件中,它不会被触发,我就是找不到原因。在我看来,一切都是一样的
TextViewCreated
就会被触发,一切正常。但是如果我将插件的某些部分导入到我自己的插件中,它不会被触发,我就是找不到原因。在我看来,一切都是一样的
我的计划是使用现有扩展的魔力,并为我的插件使用生成的XPATH字符串
Constants.XmlContentTypeName
是“XML”(也测试为“XHTML”)
[导出(typeof(IWPTextViewCreationListener))]
[TextViewRole(预定义的textViewRoles.Document)]
[ContentType(Constants.XmlContentTypeName)]
内部类XmlTextViewCreationListener:IWPTextViewCreationListener
{
私有只读XmlRepository\u存储库;
私有只读ActiveDocument\u ActiveDocument;
公共XmlTextViewCreationListener()
:这(Registry.Current.Get(),Registry.Current.Get())
{
}
公共XmlTextViewCreationListener(XmlRepository存储库,ActiveDocument ActiveDocument)
{
_存储库=存储库;
_activeDocument=activeDocument;
}
已创建公共无效文本视图(IWPTextView文本视图)
{
if(textView?.Caret==null)
{
返回;
}
_LoadXml(textView.TextSnapshot.GetText(),_activeDocument.AbsolutePath);
textView.Closed+=ResetXml;
textView.Caret.PositionChanged+=StoreCurrentNode;
}
修复了该问题。事件现在已正确触发。链接似乎已断开,您能检查并更正它吗?
[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;
}