是使用共享服务的示例。Prism我有5个模块,我使用EventAggregator模式在模块之间进行通信。在我看来,我的代码变得丑陋,在我的项目中使用EventAggregator是糟糕的设计。模块之间的通信方式有以下三种:想了解共享服务的通信方式。我找到了一篇关于PrismToolKit中的StockTrader应用程序的文章。是否有一些在Prism中使用共享服务的更轻量级和更清晰的示例,其中可以看到使用共享服务在模块之间进行通信?(可下载的代码将不胜感激)您的代码在哪些方面是丑陋的?如果您愿意,EventAggregator是一项共享服务。你把你的服务接口放在一个共享程序集中,然后一个模块可以将数据推送到服务中,另一个模块从服务中获取数据。编辑:共享程序集publicinterfaceIMySharedService{voidAddData(objectnewData);对象GetData();事件System.Action数据到达;}第一个沟通模块//此类必须从统一容器中解析,可能是通过AutoWireViewModel内部类SomeClass{publicSomeClass(IMySharedServicesharedService){_sharedService=sharedService;}publicvoidPerformImport(IEnumerabledata){foreach(varitemindata)_sharedService.AddData(item);}私有只读IMySharedService_sharedService;}第二通信模块//此类必须从与SomeClass相同的统一容器中解析(见上文)internalclassSomeOtherClass{publicSomeOtherClass(IMySharedServicesharedService){_sharedService=sharedService;_sharedService.DataArrived+=OnNewData;}publicvoidProcessData(){varitem=_sharedService.GetData();如果(项目==空)返回;//对项目做一些事情...}privatereadonlyIMySharedService_sharedService;私人无效OnNewData(objectitem){//Dosomethingwiththeitem...}}一些其他模块的初始化//这提供了将被注入SomeClass和SomeOtherClass的共享服务的实例_unityContainer.RegisterType(newContainerControlledLifetimeManager());GitHub上的PrismLibraryrepo包含最新版本的StockTrader示例应用程序以及服务示例和源代码供您查看和下载https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/StockTraderRI的以上是C#学习教程:使用共享服务的例子。如果Prism分享的所有内容对您有用,需要进一步了解C#学习教程,希望您多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
