弹窗事件定义:
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"));
}
}