当前位置: 首页 > news >正文

记录一次Prism9隐式注册引发的事件聚合器失效问题

直接上代码
1、我的注册从App的RegisterTypes方法迁移到了模块

 public class AccountModule : IModule{public void OnInitialized(IContainerProvider containerProvider){}public void RegisterTypes(IContainerRegistry containerRegistry){containerRegistry.RegisterSingleton<LoginAccount>();}}

虽然我是立即执行的引用模块

 protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog){Logger.WriteLocal("开始加载dll");Assembly.LoadFrom(@"Music.Core.dll");Logger.WriteLocal("开始加载模块");moduleCatalog.AddModule<AccountModule>(InitializationMode.WhenAvailable);var m = moduleCatalog.Modules.First(me => me.ModuleName == nameof(AccountModule));Logger.WriteLocal($"🔍 模块 {m.ModuleName} 状态 = {m.State}");}

但是log显示NotStarted
我虽然登录的时候写了

 public class LoginWindowViewModel : BindableBase{private readonly LoginAccount _loginAccount;public LoginWindowViewModel(IEventAggregator eventAggregator, LoginAccount loginAccount){_eventAggregator = eventAggregator;LoginCommand = new DelegateCommand<Window>(ExecuteLogin);}private IEventAggregator _eventAggregator;private void ExecuteLogin(Window window){_logger.WriteLocal("🚀 准备发布 LoginEvent");_eventAggregator.GetEvent<LoginEvent>().Publish(window);}public ICommand LoginCommand { get; set; }}

但是事件聚合器

 public class LoginAccount{private readonly IEventAggregator _eventAggregator;private ITangdaoLogger _logger = TangdaoLogger.Get(typeof(LoginAccount));public LoginAccount(IEventAggregator eventAggregator){_eventAggregator = eventAggregator;var token = _eventAggregator.GetEvent<LoginEvent>().Subscribe(Login);_logger.WriteLocal($"✅ LoginAccount 订阅完成,token={token.GetHashCode()}");}private void Login(Window window){window.DialogResult = true;}}

并没有进入
因为隐式注册的时候LoginAccount的生命周期是短暂的或者没有传递,此时,我改为

  private readonly LoginAccount _loginAccount;public LoginWindowViewModel(IEventAggregator eventAggregator, LoginAccount loginAccount){_eventAggregator = eventAggregator;_loginAccount = loginAccount;LoginCommand = new DelegateCommand<Window>(ExecuteLogin);}

我只是引入了字段,现在成功了
说明1、可能被GC回收了
2、Prism默认的解析机制,与微软的ServiceCollection不同,ServiceCollection是注册了类才能解析,而Prism具有隐式注册机制,比如程序启动的时候

 return Container.Resolve<MainWindow>();

我们明明没有对MainWindow进行注册,但是却可以解析出来,而ServiceCollection不可以,
这个隐式注册的生命周期是瞬态的,导致拿到的不是同一个

http://www.zskr.cn/news/41317.html

相关文章:

  • Winfrom机器人自动寻路
  • 低代码平台基础知识
  • Win11 install CUDA 12.5
  • 机器学习-逻辑回归算法-向量版代码
  • 星期三
  • Java项目常用异常处理场景与实战指南
  • 一次性删除所有的GitHub Action记录
  • 第三十四篇
  • 2025-11-05 PQ v.Next日志记录
  • AGC与AVC是什么
  • 结构体与联合体的区别
  • 新学期每日总结(第20天)
  • 铁杆粉丝占比20251105
  • 【动态维护前 x 大元素】LeetCode 3321. 计算子数组的 x-sum II
  • 100小时学会SAP—问题9:MD03提示日期在有效工厂日历之后(请改正)
  • 100小时学会SAP—问题7:FB70提示过账码没有定义
  • 树剖
  • 如何降低大模型幻觉
  • 多智能体架构中 如何解决总控agent路由错误的问题
  • 回归(监督学习)
  • 100小时学会SAP—问题3:成本控制控制凭证的编号范围
  • 牛客2025秋季算法编程训练联赛4-提升组
  • 随机数板子 - miao
  • 在React中实现路由跳转
  • 022304105叶骋恺数据采集第二次作业
  • 2025.11.5模拟赛
  • WordPress Social Feed Gallery插件未授权信息泄露漏洞分析
  • 2025-11-3
  • 2025-11-2
  • 网页打包EXE/APK/IPA出现乱码时怎么回事?