【问题标题】:Communication between Module and SHELL in PRISM MEFPRISM MEF 中 Module 和 SHELL 之间的通信
【发布时间】:2023-03-17 08:33:01
【问题描述】:

我有一个带有接口 IModuleMainService 和一个事件的模块。

例如[Export(typeof(IModuleMainService))]

我有带接口 IMainService 的 SHELL。

例如[Export(typeof(IMainService))]

在 ModuleMain 我有 import [Import(typeof(IMainService)]

在这种情况下应用程序工作,但我想与模块和外壳通信。

我尝试将导入插入到 Shell.cs,例如

[Import]
public IModuleMainService ModuleMainService { get; set; }

但我运行应用程序时出现错误,请参见下文。如果我评论 [Import] public IModuleMainService... 应用程序运行成功。 哪里出了问题?

重点是:SHELL中Module类的Consum事件。

System.ComponentModel.Composition 警告:1:ComposablePartDefinition 'Main.Silverlight.Views.Shell' 已被拒绝。组成保持不变。由于以下错误,更改被拒绝: 合成产生了单个合成错误。下面提供了根本原因。查看 CompositionException.Errors 属性以获取更多详细信息。

1) 未找到与约束匹配的有效导出 '((exportDefinition.ContractName == "Main.ViewModels.ShellViewModel") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "Main.ViewModels.ShellViewModel" .Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))',无效的导出可能已被拒绝。

导致:无法在部件“Main.Silverlight.Views.Shell”上设置导入“Main.Silverlight.Views.Shell.ViewModel (ContractName="Main.ViewModels.ShellViewModel")”。 元素:Main.Silverlight.Views.Shell.ViewModel (ContractName="Main.ViewModels.ShellViewModel") --> Main.Silverlight.Views.Shell --> AssemblyCatalog (Assembly="Main, Version=1.0.0.0, Culture=中立,PublicKeyToken=null")

System.ComponentModel.Composition 中发生了“System.ComponentModel.Composition.ImportCardinalityMismatchException”类型的第一次机会异常

【问题讨论】:

    标签: c# shell module prism mef


    【解决方案1】:

    在棱镜编程中,模块和外壳应该是分开的。不得声明从一个模块到另一个模块或外壳的直接引用。您需要做的是使用EventAggregator 在模块或外壳之间交换消息。

    使用 IEventAggregator 的另一种方法是使用 RegionScopes 来传递消息。

    作为总结,不要在模块和外壳之间创建直接引用;使用 IEventAggregator 交换消息并在一个公共库中声明您的消息携带事件,该公共库被所有模块和外壳重用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2015-03-19
      • 2011-05-08
      • 1970-01-01
      相关资源
      最近更新 更多