我正在尝试为Visual Studio 2017开发文本编辑器扩展,我很难将外部程序集中定义的类实例导入IWpfTextEditorCreationListener的构造函数。如果我尝试将外部程序集中定义的类导入构造函数,则不会触发IWpfTextEditorCreationListener。
该扩展实现了IWpfTextEditorCreationListener接口,如下面的代码:
[ContentType("code")]
[Export(typeof(IWpfTextViewCreationListener))]
[TextViewRole(PredefinedTextViewRoles.Editable)]
public class MyTextViewCreationListener : IWpfTextViewCreationListener
{
private ExternalClassLibrary.ExternalClass clazz;
[ImportingConstructor]
public MyTextViewCreationListener(ExternalClassLibrary.ExternalClass clazz)
{
this.clazz = clazz;
}
public void TextViewCreated(IWpfTextView textView)
{
clazz.foo();
}
}
ExternalClass在另一个程序集中实现如下:
namespace ExternalClassLibrary
{
[Export]
public class ExternalClass
{
public void foo()
{
}
}
}
如果MyTextViewCreationListener的构造函数被注释掉,则会调用TextViewCreated(),但不会注释掉它,也不会调用TextViewCreated()。是否可以将外部程序集导入IWpfTextViewCreationListener?
我找到了解决方案。
我上传了一个例子给qazxsw poi。