using System; using System.Windows; using ModuleA.Event; using Prism.Commands; using Prism.Events; using Prism.Services.Dialogs; namespace ModuleA.ViewModels; public class CViewModel : IDialogAware { public DelegateCommand ConfirmCommand { get; set; } public DelegateCommand CancelCommand { get; set; } public CViewModel(IEventAggregator aggregator) { ConfirmCommand = new DelegateCommand(Confirm); CancelCommand = new DelegateCommand(Cancel); // 通过事件聚合器发布消息 var messageEvent = aggregator.GetEvent(); messageEvent.Publish("Hello World"); } private void Cancel() { DialogParameters info = new DialogParameters(); info.Add("address", "Beijing"); RequestClose?.Invoke(new DialogResult(ButtonResult.No, info)); } private void Confirm() { DialogParameters info = new DialogParameters(); info.Add("title", "标题"); RequestClose?.Invoke(new DialogResult(ButtonResult.OK, info)); } /// /// 能否展示 /// /// public bool CanCloseDialog() { return true; } /// /// 窗口关闭【这个函数是在 CanCloseDialog 触发后调用,此时 RequestClose 一直为 null】 /// public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { parameters.TryGetValue("age", out int age); Console.WriteLine(age); } public string Title { get; } public event Action? RequestClose; }