开发者

Import allways returns null, but ImportConstructor works

I am trying to use MEF in my application, but I have problem with the Import.

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

This does not work and UserService is always null.

But using the ImportContstructor in the same class works perfectly:

    [ImportingConstructor ]
    public MainWindowVM(
        IUIVisualizerService uiVisualizer,
        IViewAwareStatus viewAwareStatus,
        IMessageBoxService messageBoxService, 
        IMan开发者_开发技巧ager mwManager,
        TagItemModel tagModel,
        ILibraryModel  documentModel,
        ILibraryServices libraryServices,
        ILogServices logServices ,
        IUserServices userServices)

Can anybody help me in the issue. I already spend hours, but did not find any solution. Thanks!!!


The property will only be set by MEF after the constructor is fully executed. When are you checking if the property is null?


I'm using ChinchV2 together with MefedMVVM to create the container. Here the code, which provides the export:

[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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜