EventAggregator and ServiceLocator Issue

99封情书 提交于 2019-12-06 12:45:17

You lack the initialization code of your locator.

Either you use Prism (do you?) and you need to set up your bootstrapper correctly - http://msdn.microsoft.com/en-us/library/gg430868(PandP.40).aspx

Or you don't use Prism and you just set up the locator manually (in Main for example):

IUnityContainer container = new UnityContainer();

// register the singleton of your event aggregator
container.RegisterType<IEventAggregator, EventAggregator>( new ContainerControlledLifetimeManager() ); 

ServiceLocator.SetLocatorProvider( () => container );

then you can call in any place of your code

var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();

Edit: you have edited your question and you now mention Prism. You should then create a custom bootstrapper, register your types and run the bootstrapper.

public class CustomBootstrapper : UnityBootstrapper 
{
}

and call

var bootstrapper = new CustomBootstrapper();
bootstrapper.Run();

in the starting routine of your application. From what I remember, UnityBootstrapper registers the IEventAggregator as singleton so you don't have to repeat that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!