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"));
    }
}
相关推荐
敲代码的 蜡笔小新7 天前
【行为型之中介者模式】游戏开发实战——Unity复杂系统协调与通信架构的核心秘诀
unity·设计模式·c#·中介者模式
周努力.9 天前
设计模式之中介者模式
设计模式·中介者模式
Cuit小唐9 天前
C++ 中介者模式详解
中介者模式
碎梦归途22 天前
23种设计模式-行为型模式之中介者模式(Java版本)
java·jvm·设计模式·中介者模式·软件设计师
程序员JerrySUN22 天前
设计模式每日硬核训练 Day 17:中介者模式(Mediator Pattern)完整讲解与实战应用
microsoft·设计模式·中介者模式
CHQIUU24 天前
Java 设计模式心法之第25篇 - 中介者 (Mediator) - 用“中央协调”降低对象间耦合度
java·设计模式·中介者模式
Pasregret1 个月前
中介者模式:解耦对象间复杂交互的设计模式
设计模式·交互·中介者模式
听闻风很好吃1 个月前
Java设计模式之中介者模式:从入门到架构级实践
java·设计模式·中介者模式