【问题标题】:Visual Studio component import is null in vsix project (MEF)Visual Studio 组件导入在 vsix 项目 (MEF) 中为空
【发布时间】:2018-08-20 16:02:55
【问题描述】:

我正在编写一个 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>

【问题讨论】:

  • 您的 .vsixmanifest 文件是什么样的?
  • 您好,我在其中添加了extension.vsixmanifest的相关部分。它有一个资产作为 MefComponent
  • 如何获取/创建MyViewHost 的实例?此类不实现任何已知接口,并且您不会将其导出为 MEF 组件。似乎这不是通过 MEF 组件注册表检索到的,因此无法填充导入。

标签: c# visual-studio mef visual-studio-extensions visual-studio-sdk


【解决方案1】:

我根据@nejcs 响应和gist 找到了答案。 我会发布解决方案:

[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必须在此扩展命令类中导入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多