【问题标题】:Xamarin forms Prism how can I use IEventAggregator in iOS projectXamarin 形成 Prism 如何在 iOS 项目中使用 IEventAggregator
【发布时间】:2017-10-14 02:03:21
【问题描述】:

我的问题是如何在 Xamarin.Forms 的 iOS 项目的类中使用 IEventAggregator 来发布消息。

IEventAggregator 作为构造函数参数传递似乎不起作用。似乎无法在iOS项目中解决依赖关系。那么如何解决iOS项目中IEventAggregator的依赖关系,以便在类中使用发布。

我正在使用 Prism 6.3.0

【问题讨论】:

    标签: xamarin.forms prism


    【解决方案1】:

    PrismApplication 的 Container 是一个公共属性。因此,您可以访问容器来解决您的依赖关系,例如:

    var ea = ((App)Application.Current).Container.Resolve<IEventAggregator>();
    ea.GetEvent<SomeEvent>().Publish(somePayload);
    

    【讨论】:

    • 我收到编译错误。我正在尝试在 iOS 项目中使用您在我的课程中编写的代码,错误是:“应用程序”是一种在当前上下文中无效的类型。该项目是一个没有任何功能的新项目,只是添加了棱镜。Application 的类型为App5.iOS.Application,App 的类型为App5.App
    • 您不应该引用 iOS 应用程序...请确保您引用的是 Xamarin.Forms.Application
    • 对不起,我没听懂。在哪一部分我不应该引用 iOS 应用程序?我写的正是你写的代码。
    • 您从 PrismApplication 派生的应用程序与 iOS 无关...我提供的代码引用了该应用程序,它最终是 Xamarin.Forms.Application...由于您的命名空间在您的 iOS 项目中引用 Application 类是错误的。有很多方法可以解决您遇到的问题... 1) 完全合格的应用程序。 2) 添加using Application = Xamarin.Forms.Application;
    • 好的,它有效。代码类似于var ea = ((App)Xamarin.Forms.Application.Current).Container.Resolve&lt;IEventAggregator&gt;();。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多