MessageDialog.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Windows;
  3. namespace TeamAAS.Dialogs.Dialogs
  4. {
  5. /// <summary>统一的消息、警告、异常和确认对话框。</summary>
  6. public partial class MessageDialog
  7. {
  8. public bool Result { get; private set; }
  9. /// <summary>三键对话框结果:true=是 / false=否 / null=取消(仅 SetupAskYesNoCancel 使用)</summary>
  10. public bool? YesNoCancelResult { get; private set; }
  11. 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";
  12. private const string WarningIcon = "M12,2L1,21H23M12,6L19.53,19H4.47M11,10V14H13V10M11,16V18H13V16";
  13. 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";
  14. private const string QuestionIcon = InfoIcon;
  15. private const string ColorInfo = "#2196F3";
  16. private const string ColorWarning = "#FF9800";
  17. private const string ColorError = "#F44336";
  18. private const string ColorQuestion = "#2196F3";
  19. public MessageDialog()
  20. {
  21. InitializeComponent();
  22. UiScaler.Attach(this); // 分辨率/DPI 自适应
  23. }
  24. public void SetupInfo(string message, string title = "提示")
  25. {
  26. Configure(message, title, InfoIcon, ColorInfo, "确定", null, null);
  27. }
  28. public void SetupWarning(string message, string title = "警告")
  29. {
  30. Configure(message, title, WarningIcon, ColorWarning, "确定", null, null);
  31. }
  32. public void SetupError(string message, string title = "错误", string details = null)
  33. {
  34. Configure(message, title, ErrorIcon, ColorError, "确定", null, details);
  35. }
  36. /// <summary>显示带可展开诊断详情的异常对话框。</summary>
  37. public void SetupException(string message, Exception exception, string title = "错误")
  38. {
  39. var summary = exception == null ? message : string.Concat(message, ": ", exception.Message);
  40. SetupError(summary, title, exception?.ToString());
  41. }
  42. public void SetupConfirm(string message, string title = "确认", string okText = "确定", string cancelText = "取消")
  43. {
  44. Configure(message, title, QuestionIcon, ColorQuestion, okText, cancelText, null);
  45. }
  46. public void SetupAskYesNo(string message, string title = "请确认", string yesText = "是", string noText = "否")
  47. {
  48. SetupConfirm(message, title, yesText, noText);
  49. }
  50. /// <summary>是/否/取消三键确认。</summary>
  51. public void SetupAskYesNoCancel(string message, string title = "请确认")
  52. {
  53. YesNoCancelResult = null;
  54. Configure(message, title, QuestionIcon, ColorQuestion, "是", "取消", null);
  55. BtnNo.Visibility = Visibility.Visible;
  56. }
  57. private void Configure(string message, string title, string icon, string color, string okText, string cancelText, string details)
  58. {
  59. Result = false;
  60. YesNoCancelResult = null;
  61. Title = title;
  62. MessageText.Text = message ?? string.Empty;
  63. IconDisplay.Data = System.Windows.Media.Geometry.Parse(icon);
  64. IconDisplay.Fill = new System.Windows.Media.SolidColorBrush(
  65. (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(color));
  66. BtnOK.Content = okText;
  67. BtnOK.Visibility = Visibility.Visible;
  68. BtnNo.Visibility = Visibility.Collapsed;
  69. BtnCancel.Content = cancelText;
  70. BtnCancel.Visibility = string.IsNullOrEmpty(cancelText) ? Visibility.Collapsed : Visibility.Visible;
  71. DetailsText.Text = details ?? string.Empty;
  72. DetailsExpander.Visibility = string.IsNullOrWhiteSpace(details) ? Visibility.Collapsed : Visibility.Visible;
  73. DetailsExpander.IsExpanded = false;
  74. BtnCopyDetails.Content = "复制诊断信息";
  75. }
  76. private void BtnCopyDetails_Click(object sender, RoutedEventArgs e)
  77. {
  78. if (!string.IsNullOrWhiteSpace(DetailsText.Text))
  79. {
  80. Clipboard.SetText(DetailsText.Text);
  81. BtnCopyDetails.Content = "已复制";
  82. }
  83. }
  84. private void BtnOK_Click(object sender, RoutedEventArgs e)
  85. {
  86. Result = true;
  87. YesNoCancelResult = true;
  88. Close();
  89. }
  90. private void BtnNo_Click(object sender, RoutedEventArgs e)
  91. {
  92. Result = false;
  93. YesNoCancelResult = false;
  94. Close();
  95. }
  96. private void BtnCancel_Click(object sender, RoutedEventArgs e)
  97. {
  98. Result = false;
  99. YesNoCancelResult = null;
  100. Close();
  101. }
  102. }
  103. }