且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EventAggregator和服务定位问题

更新时间:2023-02-26 22:01:24

您没有您定位的初始化代码。

You lack the initialization code of your locator.

无论你使用棱镜(你呢?),你需要正确设置你的引导程序 - http://msdn.microsoft.com/en-us /library/gg430868(PandP.40).aspx

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>();



编辑:您编辑你的问题,你现在提棱镜。然后您应该创建一个自定义的引导程序,注册类型和运行引导程序

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 
{
}

和呼叫

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

在应用程序的启动程序。从我记得, UnityBootstrapper 注册 IEventAggregator 为单身,所以你不必重复。

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.