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"));
    }
}
相关推荐
o0向阳而生0o19 小时前
113、23种设计模式之中介者模式(21/23)
设计模式·中介者模式
LoveXming1 个月前
Chapter14—中介者模式
c++·microsoft·设计模式·中介者模式·开闭原则
Meteors.1 个月前
23种设计模式——中介者模式 (Mediator Pattern)详解
java·设计模式·中介者模式
bkspiderx2 个月前
C++设计模式之行为型模式:中介者模式(Mediator)
c++·设计模式·中介者模式
charlie1145141912 个月前
精读C++20设计模式:行为型设计模式:中介者模式
c++·学习·设计模式·c++20·中介者模式
青草地溪水旁2 个月前
设计模式(C++)详解——中介者模式(2)
c++·设计模式·中介者模式
大飞pkz2 个月前
【设计模式】中介者模式
开发语言·设计模式·c#·中介者模式
努力也学不会java2 个月前
【设计模式】中介者模式
java·设计模式·中介者模式
phdsky2 个月前
【设计模式】中介者模式
c++·设计模式·中介者模式
new_daimond2 个月前
设计模式-中介者模式详解
设计模式·中介者模式