Reputation: 1746
I'm using Caliburn.Micro with MEF in one of my projects. I can get imports in the root viewmodel. But if I want to get imports into some other classes, it will not work. For example:
[Export]
public class A
{
[Import]
static ILogger logger;
public static void SomeMethod()
{
logger.Log("foobar");
}
}
And...
[Export(typeof(ILogger))]
public class FileLogger : ILogger
{
public void Log()
{
//some implementations
}
}
When I run the application, logger is never set or just equals to null.
Weirdly, if I add an import into the root viewmodel as follows and set a breakpoint there, it works somehow:
[Import]
public ILogger logger {get; set;}
However, when I press F5 to continue, logger turns out to be null when it comes to the class A. I'm wondering why the value of logger is set to null and in which place.
Upvotes: 1
Views: 1353
Reputation: 4172
You need to use the CompositionContainer
to compose or satisfy the imports of the object.
A a = new A();
compositionContainer.ComposeParts(a);
Otherwise you can use one of the CompositionContainer.GetExportXXXX
methods. This way MEF will create and compose the object for you.
Upvotes: 1