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"));
    }
}
相关推荐
钦拆大仁4 天前
Java设计模式-中介者模式
设计模式·中介者模式
小飞侠hello5 天前
中介者模式
中介者模式
Engineer邓祥浩6 天前
设计模式学习(20) 23-18 中介者模式
学习·设计模式·中介者模式
小码过河.15 天前
设计模式——中介者模式
中介者模式
Geoking.15 天前
【设计模式】中介者模式(Mediator)详解
java·设计模式·中介者模式
胖虎120 天前
iOS中的设计模式(十)- 中介者模式(从播放器场景理解中介者模式)
设计模式·中介者模式·解耦·ios中的设计模式
刀法孜然20 天前
23种设计模式 3 行为型模式 之3.6 mediator 中介者模式
设计模式·中介者模式
a35354138225 天前
设计模式-中介者模式
c++·设计模式·中介者模式
JavaBoy_XJ1 个月前
行为型-中介者模式
中介者模式
__万波__1 个月前
二十三种设计模式(十八)--中介者模式
java·设计模式·中介者模式