2

我正在尝试在我的应用程序中使用 MEF,但导入有问题。

    [Import (typeof(IUserServices ))]
    public IUserServices UserService { get; private set; }

这不起作用,并且 UserService 始终为空。

但是在同一个类中使用 ImportContstructor 效果很好:

    [ImportingConstructor ]
    public MainWindowVM(
        IUIVisualizerService uiVisualizer,
        IViewAwareStatus viewAwareStatus,
        IMessageBoxService messageBoxService, 
        IManager mwManager,
        TagItemModel tagModel,
        ILibraryModel  documentModel,
        ILibraryServices libraryServices,
        ILogServices logServices ,
        IUserServices userServices)

任何人都可以帮助我解决这个问题。我已经花了几个小时,但没有找到任何解决方案。谢谢!!!

4

2 回答 2

1

只有在构造函数完全执行后,MEF 才会设置该属性。你什么时候检查属性是否为空?

于 2011-07-06T18:17:36.603 回答
0

我正在使用 ChinchV2 和 MefedMVVM 来创建容器。这是提供导出的代码:

[PartCreationPolicy(CreationPolicy.Shared)]
[Export (typeof(IUserServices ))]
public class TestUserServices:IUserServices 
{
    public void GetSettings(Action<HubSettings, Exception> callback)
    {
        var dPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6);
        callback(new HubSettings {DataPath = dPath}, null);
    }
}
于 2011-06-30T16:16:09.273 回答