如何创建另一个模块的类的实例,而无需在wpf中使用prism添加引用 [英] how to create an instance of a class of another module without adding reference using prism in wpf

查看:171
本文介绍了如何创建另一个模块的类的实例,而无需在wpf中使用prism添加引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Prism 4.0和MEF开发WPF应用程序.我想在模块B的类B中创建模块A的类A的实例,并想要访问类A的属性和方法,而无需在模块B中添加模块A的任何引用.不知道该怎么做.

I am working on WPF application using Prism 4.0 and MEF. I want to create an instance of class A of Module A in Class B of Module B and want to access properties and methods of Class A without adding any reference of Module A in Module B. I know that prism provide this functionality, but don't know how to do it.

我们已经在配置文件中指定了所有程序集,如下所示:

We've specified all the assemblies in config file as follows:

<modules>
    <module assemblyFile="ModuleA.dll" moduleType="ModuleA.ModuleA, ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleA" startupLoaded="true"/>
    <module assemblyFile="ModuleB.dll" moduleType="ModuleB.ModuleB,ModuleB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleB" startupLoaded="true"/>
</modules>

所有程序集都以功能区面板中的菜单形式加载.

All the assemblies gets loaded in the form of Menus in a ribbon panel.

推荐答案

通常,您不直接使用Prism IModule实例,它们只是用作模块dll的入口点.继续说,我假设ModuleA.dll在ModuleB.dll中实现了所需的功能.实际上确实是通常使用Prism的方式,但是解决您的问题的方式与MEF和依赖项注入更多有关:基本上,您可以为所需的任何功能创建一个接口,在A中实现该接口并使用该接口(即,不知道/不关心)在B中的位置及其实现方式.

Normally you don't use the Prism IModule instances directly, they just serve as an entry point for the module dll. Continuing on that I'm assuming ModuleA.dll implements functionality somewhere that is needed in ModuleB.dll. This is indeed how Prism is used typically, but the solution to your problem is more related to MEF and dependency injection: basically you create an interface for whatever functionality you need, implement that interface in A and use the interface (i.e. without knowing/caring where and how it's implemented in B. Example:

在SharedInterfaces项目中

in SharedInterfaces project

public interface IMenuStuff
{
  void DoSomething( .... )
}

在ModuleA项目(引用SharedInterfaces项目)中

in ModuleA project (which references SharedInterfaces project)

[Export( typeof( IMenuStuff ) ]
public class MenuStuff : IMenuStuff
{
  public void DoSomething( .... )
  {
    ...
  }
}

在ModuleB项目中(该项目也引用了SharedInterfaces项目)

in ModuleB project (which also references SharedInterfaces project)

[ModuleExport( typeof( ModuleB )]
class ModuleB : IModule
{
  [Import]
  private IMenuStuff Menu { get; set; }

  public void Initialize()
  {
    //when reaching this point, and ModuleA was loaded properly
    //Menu will have been set by MEF to the instance exported in ModuleA
  }
}

这篇关于如何创建另一个模块的类的实例,而无需在wpf中使用prism添加引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆