UProgressBar.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using NPOI.SS.Formula.Functions;
  2. using Prism.Events;
  3. using Prism.Mvvm;
  4. using Prism.Services.Dialogs;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. using TeamAAS_VP.Events;
  22. namespace TeamAAS_VP.Controls
  23. {
  24. /// <summary>
  25. /// UProgressBar.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class UProgressBar : UserControl, IDialogAware, INotifyPropertyChanged
  28. {
  29. public struct ProgressParameter
  30. {
  31. public double Maximum;
  32. public double Minimum;
  33. public string SubTitle;
  34. public string Message;
  35. public double Value;
  36. }
  37. IEventAggregator eventAggregator;
  38. #region 属性
  39. private double maximum = 100;
  40. /// <summary>
  41. /// 进度最大值
  42. /// </summary>
  43. public double Maximum
  44. {
  45. get { return maximum; }
  46. set { SetProperty(ref maximum, value); }
  47. }
  48. private double minimum = 0;
  49. /// <summary>
  50. /// 进度最小值
  51. /// </summary>
  52. public double Minimum
  53. {
  54. get { return minimum; }
  55. set { SetProperty(ref minimum, value); }
  56. }
  57. private double _value = 0;
  58. /// <summary>
  59. /// 进度当前值
  60. /// </summary>
  61. public double Value
  62. {
  63. get { return _value; }
  64. set { SetProperty(ref _value, value); }
  65. }
  66. private string subTitle = "";
  67. /// <summary>
  68. /// 副标题
  69. /// </summary>
  70. public string SubTitle
  71. {
  72. get { return subTitle; }
  73. set { SetProperty(ref subTitle, value); }
  74. }
  75. private string message = "";
  76. /// <summary>
  77. /// 信息
  78. /// </summary>
  79. public string Message
  80. {
  81. get { return message; }
  82. set { SetProperty(ref message, value); }
  83. }
  84. public string Title => "";
  85. #endregion
  86. #region 命令
  87. #endregion
  88. #region 事件
  89. public event Action<IDialogResult> RequestClose;
  90. #endregion
  91. public UProgressBar(IEventAggregator ea)
  92. {
  93. eventAggregator = ea;
  94. InitializeComponent();
  95. DataContext = this;
  96. eventAggregator.GetEvent<ProgressNotification>().Subscribe(progressChanged);
  97. }
  98. #region 方法
  99. private void progressChanged(ProgressParameter obj)
  100. {
  101. if (obj.Value < obj.Minimum)
  102. {
  103. CloseDialog("True");
  104. }
  105. else
  106. {
  107. App.Current.Dispatcher.BeginInvoke(new Action(() => {
  108. Minimum = obj.Minimum;
  109. Maximum = obj.Maximum;
  110. SubTitle = obj.SubTitle;
  111. Message = obj.Message;
  112. Value = obj.Value;
  113. }));
  114. }
  115. }
  116. protected virtual void CloseDialog(string parameter)
  117. {
  118. ButtonResult result = ButtonResult.None;
  119. if (parameter?.ToLower() == "true")
  120. result = ButtonResult.OK;
  121. else if (parameter?.ToLower() == "false")
  122. result = ButtonResult.Cancel;
  123. RaiseRequestClose(new DialogResult(result));
  124. }
  125. public virtual void RaiseRequestClose(IDialogResult dialogResult)
  126. {
  127. RequestClose?.Invoke(dialogResult);
  128. }
  129. public bool CanCloseDialog()
  130. {
  131. return true;
  132. }
  133. public void OnDialogClosed()
  134. {
  135. }
  136. public void OnDialogOpened(IDialogParameters parameters)
  137. {
  138. }
  139. #endregion
  140. #region 属性通知
  141. /// <summary>
  142. /// Occurs when a property value changes.
  143. /// </summary>
  144. public event PropertyChangedEventHandler PropertyChanged;
  145. /// <summary>
  146. /// Checks if a property already matches a desired value. Sets the property and
  147. /// notifies listeners only when necessary.
  148. /// </summary>
  149. /// <typeparam name="T">Type of the property.</typeparam>
  150. /// <param name="storage">Reference to a property with both getter and setter.</param>
  151. /// <param name="value">Desired value for the property.</param>
  152. /// <param name="propertyName">Name of the property used to notify listeners. This
  153. /// value is optional and can be provided automatically when invoked from compilers that
  154. /// support CallerMemberName.</param>
  155. /// <returns>True if the value was changed, false if the existing value matched the
  156. /// desired value.</returns>
  157. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  158. {
  159. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  160. storage = value;
  161. RaisePropertyChanged(propertyName);
  162. return true;
  163. }
  164. /// <summary>
  165. /// Checks if a property already matches a desired value. Sets the property and
  166. /// notifies listeners only when necessary.
  167. /// </summary>
  168. /// <typeparam name="T">Type of the property.</typeparam>
  169. /// <param name="storage">Reference to a property with both getter and setter.</param>
  170. /// <param name="value">Desired value for the property.</param>
  171. /// <param name="propertyName">Name of the property used to notify listeners. This
  172. /// value is optional and can be provided automatically when invoked from compilers that
  173. /// support CallerMemberName.</param>
  174. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  175. /// <returns>True if the value was changed, false if the existing value matched the
  176. /// desired value.</returns>
  177. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  178. {
  179. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  180. storage = value;
  181. onChanged?.Invoke();
  182. RaisePropertyChanged(propertyName);
  183. return true;
  184. }
  185. /// <summary>
  186. /// Raises this object's PropertyChanged event.
  187. /// </summary>
  188. /// <param name="propertyName">Name of the property used to notify listeners. This
  189. /// value is optional and can be provided automatically when invoked from compilers
  190. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  191. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  192. {
  193. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  194. }
  195. /// <summary>
  196. /// Raises this object's PropertyChanged event.
  197. /// </summary>
  198. /// <param name="args">The PropertyChangedEventArgs</param>
  199. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  200. {
  201. PropertyChanged?.Invoke(this, args);
  202. }
  203. #endregion
  204. }
  205. }