开发者

Import allways returns null, but ImportConstructor works

开发者 https://www.devze.com 2023-03-16 21:28 出处:网络
I am trying to use MEF in my application, but I have problem with the Import. [Import (typeof(IUserServices ))]

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

精彩评论

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

关注公众号