0
所以我有這樣一個類從我試圖導入某些構造函數:MEF - 沒有找到相匹配的出口的約束
[Export]
public partial class MainWindow
{
private readonly MainWindowViewModel _viewModel;
private readonly IEnumerable<IApplicationClosingListener> _applicationClosingListeners;
private readonly Lazy<IPreferencesRepository> _preferencesRepository;
[ImportingConstructor]
public MainWindow(MainWindowViewModel viewModel, IViewManager viewManager, [ImportMany] IEnumerable<IApplicationClosingListener> applicationClosingListeners, Lazy<IPreferencesRepository> preferencesRepository)
{
_doStuff();
}
}
我也導出所有我想要的類進口(MainWindowViewModel
,IViewManager
和IPreferencesRepository
)。我這個做這樣:
MainWindowViewModel
和IViewManager
類:
[Export]
[Export(typeof(IViewManager))]
[Export(typeof(IMessagePresenter))]
public class MainWindowViewModel : BaseViewModel, IViewManager, IApplicationClosingListener
{
[ImportingConstructor]
public MainWindowViewModel(
Lazy<IPreferencesRepository> preferencesRepository)
{
_doStuff();
}
最後,IPreferencesRepository
類:
[Export(typeof(IPreferencesRepository))]
public class PreferencesRepository : IPreferencesRepository
{
private readonly IPreferencesStorage _preferencesStorage;
[ImportingConstructor]
public PreferencesRepository(IPreferencesStorage preferencesStorage, IPreferencesSerializer preferencesSerializer)
{
_doStuff();
}
現在,當我試圖獲得並使用出口出現問題MainWindow
分類:
var container = new CompositionContainer(new ApplicationCatalog());
container.ComposeExportedValue((IApplication)this);
var mainWindow = container.GetExport<MainWindow>(); // here the exception is thrown.
ImportCardinalityMismatchException
被拋出。此外,在調試控制檯中,我收到三條消息,告訴我「未找到與約束匹配的導出」。
我錯過了什麼?