I'm writing a Visual Studio extension, and I need create my own IWpfTextViewHost
(CodeEditor Window). This can be done via the ITextEditorFactoryService
but this has to be loaded via the MEF framework.
But my import is always null
, but I can't seem to work out why my import is null
. I have a rought idea how MEF works, is there a method to call the CompositionContainer
of Visual Studio itself? Or does the vsix project construct this container? If so, based on what settings?
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);
}
}
Edit
this is the relevant part of the 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>
I've found the answer based on @nejcs response and this gist. I'll post the solution:
[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);
}
IMyViewHost
is an interface with the CreateViewHost
Method. The SatisfyImportsOnce
must be called in the constructor of the extension command class and the IMyViewHost
must be imported in this extension command class.