在我的App.xaml.cs中
private void InitializeContainer()
{
var catalogs = new AggregateCatalog();
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
catalogs.Catalogs.Add(catalog);
// Also adding Interactions project
catalog = new AssemblyCatalog(typeof(InteractionsService).Assembly);
catalogs.Catalogs.Add(catalog);
// initialize the main application composition host (container)
CompositionHost.Initialize(catalogs);
} 但是,当我尝试初始化对象时,如下所示:
this.InteractionsService = ServiceLocator.Current.GetInstance<IInteractionsService>();我得到了异常,我的ServiceLocator.Current为空。
我该如何让它工作呢?
发布于 2012-01-10 13:55:51
我想您在设置CompositionContainer时错过了对ComposeParts或Compose的调用。在您开始通过GetInstance解析实例之前。
这里有一个MSDN walk through here,以及其他一些here和here示例。相关的代码片段如下:
private CompositionContainer _container
//your code
var batch = new CompositionBatch();
batch.AddPart(this);
_container.Compose(batch);
//or more simply
_container.ComposeParts(this) //if this method is supported发布于 2012-04-26 02:24:52
我也有同样的问题..
找到了这个可能有帮助的东西,
http://www.itwox.com/post/Using-MEF-with-Common-Service-Locator.aspx
关键语句是
ServiceLocator.SetLocatorProvider(() => whateveryourlocatorinstanceis );
https://stackoverflow.com/questions/8798221
复制相似问题