Dialogbox.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. //using form = System.Windows.Forms;
  8. using Caliburn.Micro;
  9. namespace OpenSEMI.ClientBase
  10. {
  11. public class DialogBox
  12. {
  13. #region Folder Browser Dialog
  14. /// <summary>
  15. /// Folder Browser Dialog
  16. /// </summary>
  17. /// <returns>User Seleted Path</returns>
  18. //public static String ShowFolderBrowserDialog()
  19. //{
  20. // form.FolderBrowserDialog dialog = new form.FolderBrowserDialog();
  21. // dialog.ShowNewFolderButton = false;
  22. // dialog.RootFolder = Environment.SpecialFolder.MyComputer;
  23. // form.DialogResult result = dialog.ShowDialog();
  24. // return result == form.DialogResult.OK ? dialog.SelectedPath : String.Empty;
  25. //}
  26. ///// <summary>
  27. ///// Folder Browser Dialog
  28. ///// </summary>
  29. ///// <param name="oldPath">old path</param>
  30. ///// <param name="rootDialog">default path</param>
  31. ///// <param name="isShowNewFolderButton">show new folder button</param>
  32. ///// <returns>User Seleted Path</returns>
  33. //public static String ShowFolderBrowserDialog(String oldPath, Environment.SpecialFolder rootDialog = Environment.SpecialFolder.MyComputer, Boolean isShowNewFolderButton = false)
  34. //{
  35. // form.FolderBrowserDialog dialog = new form.FolderBrowserDialog();
  36. // dialog.ShowNewFolderButton = isShowNewFolderButton;
  37. // if (String.IsNullOrWhiteSpace(oldPath))
  38. // {
  39. // dialog.RootFolder = rootDialog;
  40. // }
  41. // else
  42. // {
  43. // dialog.SelectedPath = oldPath;
  44. // }
  45. // form.DialogResult result = dialog.ShowDialog();
  46. // return result == form.DialogResult.OK ? dialog.SelectedPath : String.Empty;
  47. //}
  48. #endregion
  49. #region Simple dialog
  50. public static DialogButton ShowError(MESSAGE msgEnum, params object[] param)
  51. {
  52. string msg = GetMsg(msgEnum);
  53. return ShowError(msg, param);
  54. }
  55. public static DialogButton ShowError(string msg, params object[] param)
  56. {
  57. return ShowDialog(DialogButton.OK, DialogType.ERROR, msg, param);
  58. }
  59. public static DialogButton ShowWarning(MESSAGE msgEnum, params object[] param)
  60. {
  61. string msg = GetMsg(msgEnum);
  62. return ShowWarning(msg, param);
  63. }
  64. public static DialogButton ShowWarning(string msg, params object[] param)
  65. {
  66. return ShowDialog(DialogButton.OK, DialogType.WARNING, msg, param);
  67. }
  68. public static DialogButton ShowInfo(MESSAGE msgEnum, params object[] param)
  69. {
  70. string msg = GetMsg(msgEnum);
  71. return ShowInfo(msg, param);
  72. }
  73. public static DialogButton ShowInfo(string msg, params object[] param)
  74. {
  75. return ShowDialog(DialogButton.OK, DialogType.INFO, msg, param);
  76. }
  77. public static bool Confirm(MESSAGE msgEnum, params object[] param)
  78. {
  79. string msg = GetMsg(msgEnum);
  80. return Confirm(msg, param);
  81. }
  82. public static bool Confirm(string msg, params object[] param)
  83. {
  84. DialogButton btn = ShowDialog(DialogButton.Yes | DialogButton.No, DialogType.CONFIRM, msg, param);
  85. if (btn == DialogButton.Yes)
  86. return true;
  87. else
  88. return false;
  89. }
  90. #endregion
  91. /// <summary>
  92. /// get message by enmu from resource
  93. /// </summary>
  94. /// <param name="msgEnum"></param>
  95. /// <returns></returns>
  96. public static string GetMsg(MESSAGE msgEnum)
  97. {
  98. //check contain key
  99. var msg = Application.Current.Resources[msgEnum.ToString()] as string;
  100. var msgs = msg.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries);
  101. string msgStr = string.Empty;
  102. int i = 0;
  103. foreach (var str in msgs)
  104. {
  105. if (i == msgs.Count() - 1)
  106. msgStr += str;
  107. else
  108. msgStr += str + Environment.NewLine;
  109. i++;
  110. }
  111. return msgStr;
  112. }
  113. public static DialogButton ShowDialog(DialogButton buttons, DialogType type, MESSAGE msgEnum, params object[] param)
  114. {
  115. string msg = GetMsg(msgEnum);
  116. return ShowDialog(buttons, type, msg, param);
  117. }
  118. public static DialogButton ShowDialog(DialogButton buttons, DialogType type, string msg, params object[] Params)
  119. {
  120. WindowManager wm = new WindowManager();
  121. MessageDialogViewModel dlg = new MessageDialogViewModel();
  122. dlg.DialogButton = buttons;
  123. if (Params != null && Params.Length > 0)
  124. msg = string.Format(msg, Params);
  125. dlg.Text = msg;
  126. dlg.DialogType = type;
  127. wm.ShowDialogWithNoStyle(dlg);
  128. return dlg.DialogResult;
  129. }
  130. }
  131. }