using System;
using System.Windows;
namespace TeamAAS.Dialogs.Dialogs
{
/// 统一的消息、警告、异常和确认对话框。
public partial class MessageDialog
{
public bool Result { get; private set; }
/// 三键对话框结果:true=是 / false=否 / null=取消(仅 SetupAskYesNoCancel 使用)
public bool? YesNoCancelResult { get; private set; }
private const string InfoIcon = "M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,17A1,1 0 0,1 11,16V11A1,1 0 0,1 12,10A1,1 0 0,1 13,11V16A1,1 0 0,1 12,17M12,7.5A1,1 0 0,1 11,6.5A1,1 0 0,1 12,5.5A1,1 0 0,1 13,6.5A1,1 0 0,1 12,7.5Z";
private const string WarningIcon = "M12,2L1,21H23M12,6L19.53,19H4.47M11,10V14H13V10M11,16V18H13V16";
private const string ErrorIcon = "M12,2C17.53,2 22,6.47 22,12C22,17.53 17.53,22 12,22C6.47,22 2,17.53 2,12C2,6.47 6.47,2 12,2M15.59,7L12,10.59L8.41,7L7,8.41L10.59,12L7,15.59L8.41,17L12,13.41L15.59,17L17,15.59L13.41,12L17,8.41L15.59,7Z";
private const string QuestionIcon = InfoIcon;
private const string ColorInfo = "#2196F3";
private const string ColorWarning = "#FF9800";
private const string ColorError = "#F44336";
private const string ColorQuestion = "#2196F3";
public MessageDialog()
{
InitializeComponent();
UiScaler.Attach(this); // 分辨率/DPI 自适应
}
public void SetupInfo(string message, string title = "提示")
{
Configure(message, title, InfoIcon, ColorInfo, "确定", null, null);
}
public void SetupWarning(string message, string title = "警告")
{
Configure(message, title, WarningIcon, ColorWarning, "确定", null, null);
}
public void SetupError(string message, string title = "错误", string details = null)
{
Configure(message, title, ErrorIcon, ColorError, "确定", null, details);
}
/// 显示带可展开诊断详情的异常对话框。
public void SetupException(string message, Exception exception, string title = "错误")
{
var summary = exception == null ? message : string.Concat(message, ": ", exception.Message);
SetupError(summary, title, exception?.ToString());
}
public void SetupConfirm(string message, string title = "确认", string okText = "确定", string cancelText = "取消")
{
Configure(message, title, QuestionIcon, ColorQuestion, okText, cancelText, null);
}
public void SetupAskYesNo(string message, string title = "请确认", string yesText = "是", string noText = "否")
{
SetupConfirm(message, title, yesText, noText);
}
/// 是/否/取消三键确认。
public void SetupAskYesNoCancel(string message, string title = "请确认")
{
YesNoCancelResult = null;
Configure(message, title, QuestionIcon, ColorQuestion, "是", "取消", null);
BtnNo.Visibility = Visibility.Visible;
}
private void Configure(string message, string title, string icon, string color, string okText, string cancelText, string details)
{
Result = false;
YesNoCancelResult = null;
Title = title;
MessageText.Text = message ?? string.Empty;
IconDisplay.Data = System.Windows.Media.Geometry.Parse(icon);
IconDisplay.Fill = new System.Windows.Media.SolidColorBrush(
(System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(color));
BtnOK.Content = okText;
BtnOK.Visibility = Visibility.Visible;
BtnNo.Visibility = Visibility.Collapsed;
BtnCancel.Content = cancelText;
BtnCancel.Visibility = string.IsNullOrEmpty(cancelText) ? Visibility.Collapsed : Visibility.Visible;
DetailsText.Text = details ?? string.Empty;
DetailsExpander.Visibility = string.IsNullOrWhiteSpace(details) ? Visibility.Collapsed : Visibility.Visible;
DetailsExpander.IsExpanded = false;
BtnCopyDetails.Content = "复制诊断信息";
}
private void BtnCopyDetails_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(DetailsText.Text))
{
Clipboard.SetText(DetailsText.Text);
BtnCopyDetails.Content = "已复制";
}
}
private void BtnOK_Click(object sender, RoutedEventArgs e)
{
Result = true;
YesNoCancelResult = true;
Close();
}
private void BtnNo_Click(object sender, RoutedEventArgs e)
{
Result = false;
YesNoCancelResult = false;
Close();
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
Result = false;
YesNoCancelResult = null;
Close();
}
}
}