using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
namespace PLCSharp.Core.Prism
{
///
/// 对话框通知基类,封装了 Prism IDialogAware 的通用逻辑
///
public class DialogAwareBase : BindableBase, IDialogAware
{
///
/// 构造对话框基类,保存事件聚合器
///
public DialogAwareBase(IContainerExtension container, IEventAggregator ea, IDialogService dialogService)
{
_EventAggregator = ea;
}
private string _Title = "";
///
/// 标题
///
public string Title
{
get { return _Title; }
set { SetProperty(ref _Title, value); }
}
private DelegateCommand _closeDialogCommand;
///
/// 关闭对话框命令
///
public DelegateCommand CloseDialogCommand => _closeDialogCommand ??= new DelegateCommand(CloseDialog);
private string _message;
///
/// 消息内容
///
public string Message
{
get { return _message; }
set { SetProperty(ref _message, value); }
}
///
/// 请求关闭对话框的句柄
///
public DialogCloseListener RequestClose { get; }
///
/// 根据参数关闭对话框并返回结果
///
/// 按钮参数(ok/yes/no/cancel 等)
protected virtual void CloseDialog(string parameter)
{
var resultstr = parameter?.ToLower();
var result = resultstr switch
{
"ok" or "true" => ButtonResult.OK,
"yes" => ButtonResult.Yes,
"no" or "false" => ButtonResult.No,
"cancel" => ButtonResult.Cancel,
"ignore" => ButtonResult.Ignore,
_ => ButtonResult.Cancel,
};
RaiseRequestClose(new DialogResult(result));
}
///
/// 触发对话框关闭事件,通知调用方结果
///
/// 对话框结果(包含按钮状态和数据)
public virtual void RaiseRequestClose(IDialogResult dialogResult)
{
RequestClose.Invoke(dialogResult);
}
///
/// 判断对话框是否可以关闭
///
/// 始终返回 true
public virtual bool CanCloseDialog()
{
return true;
}
///
/// 对话框关闭后的回调,子类可重写进行清理
///
public virtual void OnDialogClosed()
{
}
///
/// 对话框打开后的回调,解析传入的参数
///
/// 传入的参数列表,可从其中获取 message 等参数
public virtual void OnDialogOpened(IDialogParameters parameters)
{
var message = parameters.GetValue("message");
if (message != null)
{
var msg = message.Split(':');
Title = msg[0];
if (msg.Length > 1)
{
Message = msg[1];
}
}
}
///
/// 事件聚合器,用于发送消息和通知
///
protected readonly IEventAggregator _EventAggregator;
///
/// 发送错误消息到日志和界面
///
/// 错误消息内容
/// 消息类型(记录、对话框提示等)
public void SendErr(string msg, ShowType type, ErrType errType)
{
_EventAggregator.GetEvent().Publish(new()
{
ErrType = errType,
Type = type,
Target = "errLog",
Content = msg
});
}
///
/// 发送故障信息
///
/// msg
public void SendErr(string msg)
{
SendErr(msg, ShowType.Record, ErrType.Error);
}
///
/// 发送弹出故障消息
///
/// msg
public void SendErrDialog(string msg)
{
SendErr(msg, ShowType.ShowDialogAndRecord, ErrType.Error);
}
///
/// 发送普通记录信息
///
/// msg
public void SendInfo(string msg)
{
SendErr(msg, ShowType.Record, ErrType.Info);
}
///
/// 发送弹出消息
///
/// msg
public void SendInfoDialog(string msg)
{
SendErr(msg, ShowType.ShowDialogAndRecord, ErrType.Info);
}
}
}