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 天前
设计模式:中介者模式 Mediator
设计模式·中介者模式
凤山老林24 天前
Spring Boot中的中介者模式:终结对象交互的“蜘蛛网”困境
java·spring boot·后端·设计模式·中介者模式
缘来是庄1 个月前
设计模式之中介者模式
java·设计模式·中介者模式
何中应2 个月前
【设计模式-4.8】行为型——中介者模式
java·设计模式·中介者模式
qqxhb2 个月前
零基础设计模式——行为型模式 - 中介者模式
java·设计模式·go·中介者模式
lpfasd1232 个月前
中介者模式(Mediator Pattern)
中介者模式
on the way 1232 个月前
行为型设计模式之Mediator(中介者)
java·设计模式·中介者模式
暴躁哥2 个月前
深入理解设计模式之中介者模式
设计模式·中介者模式
季鸢2 个月前
Java设计模式之中介者模式详解
java·设计模式·中介者模式
张萌杰2 个月前
设计模式25——中介者模式
设计模式·中介者模式