加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Prism事件聚合器不能从单独的模块工作

发布时间:2020-12-15 04:22:26 所属栏目:百科 来源:网络整理
导读:我遇到了棱镜事件聚合器的问题.如果我订阅,并在同一模块中发布一个事件,它工作正常.像这样 – public class InfrastructureModule : IModule{ private IEventAggregator eventAggregator; public InfrastructureModule(IEventAggregator eventAggregator) {
我遇到了棱镜事件聚合器的问题.如果我订阅,并在同一模块中发布一个事件,它工作正常.像这样 –
public class InfrastructureModule : IModule
{
    private IEventAggregator eventAggregator;

    public InfrastructureModule(IEventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
        eventAggregator.GetEvent<TestEvent>().Subscribe(TestSub);
    }

    public void Initialize()
    {
        eventAggregator.GetEvent<TestEvent>().Publish("Infrastructure module");
    }

    private void TestSub(string s)
    {
        MessageBox.Show(s);
    }
}

但是,如果我在另一个模块中订阅该事件,则在调用eventAggregator.GetEvent().Publish()时没有任何反应 –

public class OtherModule : IModule
{
    private IEventAggregator eventAggregator;

    public OtherModule (IEventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
    }

    public void Initialize()
    {
        eventAggregator.GetEvent<TestEvent>().Publish("Other module");
    }
}

基础架构模块首先注册,因此问题不在于OtherModule在订户之前发布事件.任何想法都出错了?

编辑:这是我注册模块的地方

class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return new Shell();
    }

    protected override void InitializeShell()
    {
        base.InitializeShell();

        App.Current.MainWindow = (Window)this.Shell;
        App.Current.MainWindow.Show();
    }

    protected override void ConfigureModuleCatalog()
    {
        base.ConfigureModuleCatalog();

        ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;

        // Infrastructure module
        moduleCatalog.AddModule(typeof(Infrastructure.InfrastructureModule));


        moduleCatalog.AddModule(typeof(Other.OtherModule));
    }
}

解决方法

根据OP的注释,对象被实例化,然后被销毁.
这使得发布(“OtherModule”);代码什么都不做,因为监听器被破坏了.

确实,如果你将KeepSubscriberReferenceAlive设置为true,
它将起作用,因为您的EventAggregator将保留对订阅者对象(InfrastructureModule)的引用.
这并不理想,基本上你使用的是弱事件模式,你不会冒内存泄漏的风险,必须处理对象的生命周期,从而冒着内存泄漏的风险,就像常规的.NET事件一样.

不要误会我的意思,我不是说你绝对不应该使用KeepSubscriberReferenceAlive,但它应该只在极少数情况下使用.

话虽如此,您的测试用例是一个奇怪的场景:Bootstrapper将在您定义的每个模块上调用Initialize,然后您的shell不会保存这些模块.由于没有人拥有这些模块,它们就被摧毁了.

Initialize的“正常”用法是将正在初始化的模块注入Shell(或任何其他UserControl),这是有道理的:你不想初始化你不会使用的东西.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读