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

Prism使用消息总线打开窗体的案例(中介者模式)

弹窗事件定义:

using Prism.Events;public class ShowPopupEvent : PubSubEvent<string> { }

弹窗管理类(中介):

using Prism.Events;
using Prism.Services.Dialogs;public class PopupManager
{private readonly IEventAggregator _eventAggregator;private readonly IDialogService _dialogService;public PopupManager(IEventAggregator eventAggregator, IDialogService dialogService){_eventAggregator = eventAggregator;_dialogService = dialogService;_eventAggregator.GetEvent<ShowPopupEvent>().Subscribe(ShowDialog);}private void ShowDialog(string dialogName){_dialogService.ShowDialog(dialogName, null, result => {});}
}

启动和IOC注入:

 Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;public class Bootstrapper : PrismBootstrapper
{protected override void RegisterTypes(IContainerRegistry containerRegistry){containerRegistry.RegisterDialog<LoginDialog>("LoginDialog");containerRegistry.RegisterDialog<SettingsDialog>("SettingsDialog");containerRegistry.RegisterSingleton<PopupManager>();containerRegistry.RegisterSingleton<IEventAggregator, EventAggregator>();}
}

发布消息:

using Prism.Commands;
using Prism.Events;public class MainViewModel
{private readonly IEventAggregator _eventAggregator;public DelegateCommand ShowLoginCommand { get; }public DelegateCommand ShowSettingsCommand { get; }public MainViewModel(IEventAggregator eventAggregator){_eventAggregator = eventAggregator;ShowLoginCommand = new DelegateCommand(() => _eventAggregator.GetEvent<ShowPopupEvent>().Publish("LoginDialog"));ShowSettingsCommand = new DelegateCommand(() =>_eventAggregator.GetEvent<ShowPopupEvent>().Publish("SettingsDialog"));}
}

相关文章:

  • 大数据场景下数据导出的架构演进与EasyExcel实战方案
  • Android 性能优化入门(二)—— 内存优化
  • Python Requests库完全指南:从入门到精通
  • 系统安全及应用
  • Unity学习总结篇(1)关于各种坐标系
  • svn: E170013 和 svn: E120171 的问题
  • 【蓝桥杯省赛真题52】python连续正整数和 第十五届蓝桥杯青少组Python编程省赛真题解析
  • 历史数据分析——宁波海运
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | Expanding Cards (展开式卡片)
  • 产品周围的几面墙
  • 大模型中的Token机制深度解析
  • [ctfshow web入门] web122
  • 安全漏洞频发,如何加强防护措施?
  • IntelliJ IDEA设置编码集
  • 普通用户的服务器连接与模型部署相关记录
  • 代码随想录算法训练营 Day49 图论Ⅰ 深度优先与广度优先
  • 基于 Zookeeper 部署 Kafka 集群
  • Linux系统:ext2文件系统的核心概念和结构
  • 什么是open BMC?
  • GO语言(一期)常用关键字总结
  • 周国辉谈花开岭现象 :年轻的公益人正在用行动点亮希望
  • 广东信宜一座在建桥梁暴雨中垮塌,镇政府:未造成人员伤亡
  • 坐标大零号湾科创策源区,上海瑞金医院闵行院区正式启动建设
  • 广西鹿寨一水文站“倒刺扶手”存安全隐患,官方通报处理情况
  • 信俗与共:清代新疆回疆儒释道庙宇的中华政教
  • 夜读丨什么样的前程值得把春天错过