Files
ADP/ADP/ViewModels/Dialogs/MessageBoxViewModel.cs
T
2026-08-27 14:26:22 +08:00

147 lines
4.8 KiB
C#

using UIShare.PubEvent;
using UIShare.ViewModelBase;
using System;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace ADP.ViewModels.Dialogs
{
public class MessageBoxViewModel : DialogViewModelBase
{
#region 属性
private string _Title;
public string Title
{
get => _Title;
set => SetProperty(ref _Title, value);
}
private string _Message = "";
public string Message
{
get => _Message;
set => SetProperty(ref _Message, value);
}
private ImageSource _Icon = new System.Windows.Media.Imaging.BitmapImage(new System.Uri("pack://application:,,,/Images/info.png"));
public ImageSource Icon
{
get => _Icon;
set => SetProperty(ref _Icon, value);
}
private bool _ShowYes;
public bool ShowYes
{
get => _ShowYes;
set => SetProperty(ref _ShowYes, value);
}
private bool _ShowNo;
public bool ShowNo
{
get => _ShowNo;
set => SetProperty(ref _ShowNo, value);
}
private bool _ShowOk;
public bool ShowOk
{
get => _ShowOk;
set => SetProperty(ref _ShowOk, value);
}
private bool _ShowCancel;
public bool ShowCancel
{
get => _ShowCancel;
set => SetProperty(ref _ShowCancel, value);
}
#endregion
#region 命令
public ICommand YesCommand { get; set; }
public ICommand NoCommand { get; set; }
public ICommand OkCommand { get; set; }
public ICommand CancelCommand { get; set; }
#endregion
public DialogCloseListener RequestClose { get; set; }
/// <summary>
/// 自动关闭定时器:自动关闭秒数大于 0 时启动,到时自动关闭弹窗(同时解除阻塞等待)
/// </summary>
private DispatcherTimer _autoCloseTimer;
public MessageBoxViewModel(IContainerProvider containerProvider):base(containerProvider)
{
YesCommand = new DelegateCommand(OnYes);
NoCommand = new DelegateCommand(OnNo);
OkCommand = new DelegateCommand(OnOk);
CancelCommand = new DelegateCommand(OnCancel);
}
private void CloseDialog(ButtonResult result)
{
var parameters = new DialogParameters();
RequestClose.Invoke(new DialogResult(result));
}
private void OnYes() => CloseDialog(ButtonResult.Yes);
private void OnNo() => CloseDialog(ButtonResult.No);
private void OnOk() => CloseDialog(ButtonResult.OK);
private void OnCancel() => CloseDialog(ButtonResult.Cancel);
#region Prism Dialog 规范
public bool CanCloseDialog() => true;
public override void OnDialogClosed()
{
_autoCloseTimer?.Stop();
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
}
public override void OnDialogOpened(IDialogParameters parameters)
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(true);
Title = parameters.GetValue<string>("Title");
Message = parameters.GetValue<string>("Message");
var iconKey = parameters.GetValue<string>("Icon"); // info / error / warn
var bmp = (ImageSource)new System.Windows.Media.Imaging.BitmapImage(new System.Uri(iconKey switch
{
"info" => "pack://application:,,,/Images/info.png",
"error" => "pack://application:,,,/Images/error.png",
"warn" => "pack://application:,,,/Images/warning.png",
_ => "pack://application:,,,/Images/info.png"
}));
bmp.Freeze();
Icon = bmp;
ShowYes = parameters.GetValue<bool>("ShowYes");
ShowNo = parameters.GetValue<bool>("ShowNo");
ShowOk = parameters.GetValue<bool>("ShowOk");
ShowCancel = parameters.GetValue<bool>("ShowCancel");
// 自动关闭:秒数大于 0 时启动定时器,到时自动关闭(供命令库 弹窗 命令的自动关闭参数使用)
if (parameters.TryGetValue("AutoCloseSeconds", out double autoCloseSeconds) && autoCloseSeconds > 0)
{
_autoCloseTimer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(autoCloseSeconds)
};
_autoCloseTimer.Tick += (s, e) =>
{
_autoCloseTimer.Stop();
RequestClose.Invoke(new DialogResult(ButtonResult.OK));
};
_autoCloseTimer.Start();
}
}
#endregion
}
}