繁体   English   中英

vsix项目(MEF)中的Visual Studio组件导入为空

[英]Visual Studio component import is null in vsix project (MEF)

我正在编写Visual Studio扩展,我需要创建自己的IWpfTextViewHost (“代码编辑器”窗口)。 这可以通过ITextEditorFactoryService完成,但是必须通过MEF框架加载。

但是我的导入始终为null ,但是我似乎无法弄清楚为什么我的导入为null 我对MEF的工作原理有一个大概的了解,有没有一种方法可以调用Visual Studio本身的CompositionContainer 还是vsix项目构造了这个容器? 如果是这样,基于什么设置?

public class MyViewHost {

   [Import]
   public ITextEditorFactoryService TextEditorFactory { get; set; }

   public IWpfTextViewHost CreateViewHost(ITextBuffer textBuffer) 
   {
      var textView = this.TextEditorFactory.CreateTextView(textBuffer);
      return this.TextEditorFactory.CreateTextViewHost(textView, true);
   }

}

编辑这是extension.vsixmanifest的相关部分。

  <Assets>
      <Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="Build%CurrentProject%" Path="|dllName;PkgdefProjectOutputGroup|" />
      <Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" Path="dllName" />
  </Assets>

我已经基于@nejcs响应和这个要点找到了答案。 我将发布解决方案:

[Export(typeof(IMyViewHost))]
public class MyViewHost : IMyViewHost
{
    [Import]
    public ITextEditorFactoryService TextEditorFactory { get; set; }

    public IWpfTextViewHost CreateViewHost(ITextBuffer textBuffer)
    {
        var textView = this.TextEditorFactory.CreateTextView(textBuffer);
        return this.TextEditorFactory.CreateTextViewHost(textView, true);
    }

IMyViewHostCreateViewHost方法的接口。 SatisfyImportsOnce必须在扩展命令类的构造函数被调用,而IMyViewHost必须在此扩展命令类进口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM