Xamarin Forms IEventAggregator in iOS class crash the application

时间:2017-10-12 09:49:05

标签: xamarin.forms prism

I'm using Xamarin.Forms and Prism 6.3.0

I have a class in the iOS project where I want to use the IEventAggregator. When I'm passing the IEventAggregator as a parameter to the constructor of the class, the application crashes.

A clarification, I'm trying to publish from the iOS class, not to subscribe to an event published by Xamarin.Forms.

How can I use the IEventAggregator inside a class in iOS project of Xamarin.Forms?

2 个答案:

答案 0 :(得分:0)

我不知道是否有更优雅的解决方案,但有一个正在运作。您必须使用正确的注入参数手动将依赖项添加到容器中。

在您的平台特定项目中(在这种情况下为iOS,但它也适用于Android)更改以下行

.modal-body,
.modal-footer,
.modal-header {
  background-color: white;
}

.modal-backdrop{
  background-color: rgba(0, 0, 0, .99);
}

this.LoadApplication(new App());

鉴于您的var app = new App(); this.LoadApplication(); App,您现在可以访问PrismApplication并手动注册您的类型

IUnityContainer

现在Unity应该能够正确解析var app = new App(); app.Container.RegisterType(typeof(IServiceService), typeof(MyServiceService), string.Empty, new ContainerControlledLifetimeManager(), new InjectionConstructor(new ResolvedParameter(typeof(IEventAggregator)))); this.LoadApplication(app); 并且您的应用不应再崩溃了。也许你必须删除

MyServiceService

但如果它没有被移除,我没有尝试过它。

答案 1 :(得分:0)

问题的答案是here

简而言之,在iOS项目的类中添加using语句using Microsoft.Practices.Unity;并在类的约束中添加

var ea = ((App)Xamarin.Forms.Application.Current).Container.Resolve<IEventAggregator>();

以获取EventAggregator。

然后你可以用它来发布一条消息,即

ea.GetEvent<SomeEvent>().Publish(somePayload);