C# WPF,Prism:如何访问其他模块中的非静态方法?

C# WPF,Prism:如何访问其他模块中的非静态方法?,c#,wpf,module,prism,C#,Wpf,Module,Prism,据我所知,Prism不保留对创建的模块实例的引用 当我无法访问当前正在使用的模块对象的实例时,如何从另一个模块中的一个模块访问非静态方法 Edit:我通过从视图中检索datacontext并将其转换到所使用的模型(我想要的方法所在的位置)来实现它。不确定这是否是一种好的做法 IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>(); IRegion region = reg

据我所知,Prism不保留对创建的模块实例的引用

当我无法访问当前正在使用的模块对象的实例时,如何从另一个模块中的一个模块访问非静态方法

Edit:我通过从视图中检索datacontext并将其转换到所使用的模型(我想要的方法所在的位置)来实现它。不确定这是否是一种好的做法

IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

IRegion region = regionManager.Regions["RegionName"]; ;

Module.Views.View currentView= null;
foreach (Module.Views.View view in region.ActiveViews)
{
  currentView = view;
}

var model = (Module.Model)currentView.DataContext;

mode.Method();
IRegionManager regionManager=ServiceLocator.Current.GetInstance();
IRegion region=regionManager.Regions[“RegionName”];
Module.Views.View currentView=null;
foreach(region.activeview中的Module.Views.View视图)
{
当前视图=视图;
}
var模型=(Module.model)currentView.DataContext;
mode.Method();

为什么我们不能将模块实例注册到容器中

public class ModuleA : IModule
{
    private readonly IUnityContainer _container;

    public ModuleA (IUnityContainer container)
    {            
        _container = container;           
    }

    #region Implementation of IModule

    public void Initialize()
    {
         _container.RegisterInstance<IModule>("ModuleA ",this);
    }

    #endregion
}
公共类模块a:IModule
{
专用只读IUnityContainer\u容器;
公共模块A(IUnityContainer容器)
{            
_容器=容器;
}
#IModule的区域实现
公共无效初始化()
{
_容器注册状态(“ModuleA”,this);
}
#端区
}
并使用

 var module = _container.Resolve<IModule>("ModuleA ");
var模块=_container.Resolve(“ModuleA”);
模块定义(也称为实现
IModule
的类)不包含任何值得调用的方法,但框架调用的
Initialize
方法除外

如果您想在一个模块中实现一个方法并从另一个模块中使用它(或者在同一个模块中,也就是说,这无关紧要),那么创建一个类并实现一个接口

例如:

public interface IMyService
{
    void MyMethod();
}

internal class MyImplementation : IMyService
{
    #region IMyService
    public void MyMethod()
    {
        // do something useful ModuleA's way
    }
    #endregion
}

internal class ModuleA : IModule
{
    public ModuleA( IUnityContainer container )
    {
        _container = container;
    }

    #region IModule
    public void Initialize()
    {
        _container.RegisterType<IMyService, MyImplementation>();
    }
    #endregion

    #region private
    private readonly IUnityContainer _container;
    #endregion
}

internal class SomeClass
{
    public SomeClass( IMyService myService )
    {
        _myService = myService;
    }

    public void SomeMethod()
    {
        // use ModuleA's method here:
        _myService.MyMethod();
    }

    #region private
    private readonly IMyService _myService;
    #endregion
}
公共接口IMyService
{
void MyMethod();
}
内部类MyImplementation:IMyService
{
#区域IMyService
公共方法()
{
//以我的方式做一些有用的事情
}
#端区
}
内部类模块a:IModule
{
公共模块A(IUnityContainer容器)
{
_容器=容器;
}
#区域模
公共无效初始化()
{
_container.RegisterType();
}
#端区
#地区私人
专用只读IUnityContainer\u容器;
#端区
}
内部类
{
公共类(IMyService myService)
{
_myService=myService;
}
公共方法()
{
//在此处使用ModuleA的方法:
_myService.MyMethod();
}
#地区私人
私有只读IMyService\u myService;
#端区
}

事件聚合器是模块间通信的最佳选择

您可以使用PRISM框架中包含的
事件聚合器
从一个模块发送消息,然后从另一个模块接收消息。反问:为什么会有人想这样做?使用模块定义连接模块的应用程序部分。一旦完成了,就不再需要模块定义,这就是为什么prism不保留引用的原因…即使我不知道。。为什么需要模块实例。我刚才回答了如何获取实例的问题,但我同意,当涉及到设计时,不建议使用它,最好的方法是移动到不同的类并注册该类,正如您在另一篇文章中所解释的。这取决于您想要做什么。对于“火与忘”式的单向沟通,是的,但如果你想要答案,我会选择其他方式。