【发布时间】:2017-04-26 14:30:15
【问题描述】:
在 WPF 中,我可以使用 MEF (System.ComponentModel.Composition) 导入对私有属性的依赖项,如下所示:
[Import]
private IConfigService _service { get; set; }
我在 UWP 应用程序中通过 Microsoft.Composition NuGet 包使用 MEF,如果我的属性像这样公开,它可以正常工作:
[Import]
public IConfigService _service { get; set; }
但如果我将属性设为私有,它就不起作用。它不会抛出CompositionException,它只是将属性保留为null。有没有办法让它与私有属性一起使用?
更新
在对象上调用 CompositionHost.SatisfyImports 可以成功解决其依赖关系,但不能解决更远的依赖关系。在下面的示例中,在视图模型上调用 SatifyImports 将解析其高级服务,但不会解析低级服务。
[Export]
public class ViewModel
{
[Import]
private IHighLevelService _service { get; set; }
}
public interface IHighLevelService { }
[Export(typeof(IHighLevelService))]
public class HighLevelService : IHighLevelService
{
[Import]
private ILowLevelService _lowLevelService { get; set; }
}
public interface ILowLevelService { }
[Export(typeof(ILowLevelService))]
public class LowLevelService : ILowLevelService { }
【问题讨论】:
-
您是否在创建的 CompositionHost 对象上调用了
SatisfyImports方法以让 MEF 设置`_service` 属性? -
谢谢。这有点帮助,但这不是全部答案。请查看更新后的问题。