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

弹窗事件定义:

cs 复制代码
using Prism.Events;

public class ShowPopupEvent : PubSubEvent<string> { }

弹窗管理类(中介):

cs 复制代码
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注入:

cs 复制代码
 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>();
    }
}

发布消息:

cs 复制代码
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"));
    }
}
相关推荐
砍光二叉树8 天前
【设计模式】行为型-中介者模式
设计模式·中介者模式
Yu_Lijing15 天前
基于C++的《Head First设计模式》笔记——中介者模式
笔记·设计模式·中介者模式
一个儒雅随和的男子21 天前
复杂业务的解决之道,如何使用“中介者模式(Mediator Pattern)”解决复杂业务场景
microsoft·中介者模式
我是苏苏25 天前
Web开发:使用MediatR包实现中介者模式,避免组件之间直接通信
前端·中介者模式
知无不研1 个月前
中介者模式
c++·设计模式·中介者模式
逆境不可逃1 个月前
【从零入门23种设计模式17】行为型之中介者模式
java·leetcode·microsoft·设计模式·职场和发展·中介者模式
Anurmy1 个月前
设计模式之中介者模式
设计模式·中介者模式
蜜獾云1 个月前
设计模式之中介者模式:让互相调用的模块之间解耦合
microsoft·设计模式·中介者模式
阿闽ooo2 个月前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
J_liaty2 个月前
23种设计模式一中介者模式
设计模式·中介者模式