CustomProgressbar.xaml.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Animation;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace ivf_tl_Operate.CustomUserControls
  20. {
  21. /// <summary>
  22. /// CustomProgressbar.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class CustomProgressbar : UserControl
  25. {
  26. /// <summary>
  27. /// 进度条变化事件
  28. /// </summary>
  29. public event Action<double> ProChangendEvent;
  30. /// <summary>
  31. /// 操作之前发生的事件
  32. /// </summary>
  33. public event Action BeforeInteractionEvent;
  34. /// <summary>
  35. /// 操作进度条之后发生的事件
  36. /// </summary>
  37. public event Action<double> AfterInteractionEvent;
  38. /// <summary>
  39. /// 获取发育时长事件
  40. /// </summary>
  41. public event Func<double, string> LengthDevelopmentEvent;
  42. public CustomProgressbar()
  43. {
  44. InitializeComponent();
  45. this._videoPro.AddHandler(Slider.PreviewMouseLeftButtonUpEvent, new MouseButtonEventHandler(_videoPro_PreviewMouseLeftButtonUp), true);
  46. this._videoPro.AddHandler(Slider.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(_videoPro_PreviewMouseLeftButtonDown), true);
  47. }
  48. public bool IsUpdata
  49. {
  50. get { return (bool)GetValue(IsUpdataProperty); }
  51. set { SetValue(IsUpdataProperty, value); }
  52. }
  53. // Using a DependencyProperty as the backing store for IsUpdata. This enables animation, styling, binding, etc...
  54. public static readonly DependencyProperty IsUpdataProperty =
  55. DependencyProperty.Register("IsUpdata", typeof(bool), typeof(CustomProgressbar), new PropertyMetadata(false));
  56. /// <summary>
  57. /// 当前图片发育时长
  58. /// </summary>
  59. public string CurrentFaYuTimeString
  60. {
  61. get { return (string)GetValue(CurrentFaYuTimeStringProperty); }
  62. set { SetValue(CurrentFaYuTimeStringProperty, value); }
  63. }
  64. // Using a DependencyProperty as the backing store for CurrentFaYuTimeString. This enables animation, styling, binding, etc...
  65. public static readonly DependencyProperty CurrentFaYuTimeStringProperty =
  66. DependencyProperty.Register("CurrentFaYuTimeString", typeof(string), typeof(CustomProgressbar), new PropertyMetadata("00h:00m"));
  67. public double VideoAllTime
  68. {
  69. get { return (double)GetValue(VideoAllTimeProperty); }
  70. set { SetValue(VideoAllTimeProperty, value); }
  71. }
  72. // Using a DependencyProperty as the backing store for VideoAllTime. This enables animation, styling, binding, etc...
  73. public static readonly DependencyProperty VideoAllTimeProperty =
  74. DependencyProperty.Register("VideoAllTime", typeof(double), typeof(CustomProgressbar), new PropertyMetadata(1d));
  75. public double VideoCurrentTime
  76. {
  77. get { return (double)GetValue(VideoCurrentTimeProperty); }
  78. set { SetValue(VideoCurrentTimeProperty, value); }
  79. }
  80. // Using a DependencyProperty as the backing store for VideoCurrentTime. This enables animation, styling, binding, etc...
  81. public static readonly DependencyProperty VideoCurrentTimeProperty =
  82. DependencyProperty.Register("VideoCurrentTime", typeof(double), typeof(CustomProgressbar), new PropertyMetadata(0d, new PropertyChangedCallback(VideoCurrentTimePropertyChangedCallback)));
  83. private static void VideoCurrentTimePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  84. {
  85. if (!(d is CustomProgressbar source)) return;
  86. if (!source.IsUpdataFun(source)) return;
  87. if (source.isInChange) return;
  88. if (!(e.NewValue is double newTime)) return;
  89. source._PlayTime.Text = source.RevertToTime(newTime);
  90. source._AllTime.Text = source.RevertToTime(source.VideoAllTime - newTime);
  91. source._videoPro.Value = newTime;
  92. }
  93. /// <summary>
  94. /// 进度条操作开始
  95. /// </summary>
  96. /// <param name="sender"></param>
  97. /// <param name="e"></param>
  98. /// <exception cref="NotImplementedException"></exception>
  99. private void _videoPro_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  100. {
  101. if (!IsUpdataFun(this)) return;
  102. BeforeInteractionEvent?.Invoke();
  103. }
  104. /// <summary>
  105. /// 进度条操作结束
  106. /// </summary>
  107. /// <param name="sender"></param>
  108. /// <param name="e"></param>
  109. /// <exception cref="NotImplementedException"></exception>
  110. private void _videoPro_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  111. {
  112. if (!IsUpdataFun(this)) return;
  113. AfterInteractionEvent?.Invoke(this._videoPro.Value);
  114. }
  115. /// <summary>
  116. /// 开始拖动进度条
  117. /// </summary>
  118. /// <param name="sender"></param>
  119. /// <param name="e"></param>
  120. private void _videoPro_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
  121. {
  122. if (!IsUpdataFun(this)) return;
  123. BeforeInteractionEvent?.Invoke();
  124. }
  125. /// <summary>
  126. /// 拖动进度条
  127. /// </summary>
  128. /// <param name="sender"></param>
  129. /// <param name="e"></param>
  130. private void _videoPro_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
  131. {
  132. if (!IsUpdataFun(this)) return;
  133. ProChangendEvent?.Invoke(this._videoPro.Value);
  134. }
  135. bool isInChange = false;
  136. double newTime = 0d;
  137. string newFayu = "00h:00m";
  138. /// <summary>
  139. /// 进度条改变
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void _videoPro_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  144. {
  145. if (!IsUpdataFun(this)) return;
  146. newTime = this._videoPro.Value;
  147. this._PlayTime.Text = RevertToTime(newTime);
  148. this._AllTime.Text = RevertToTime(VideoAllTime - newTime);
  149. isInChange = true;
  150. VideoCurrentTime = newTime;
  151. isInChange = false;
  152. newFayu = LengthDevelopmentEvent?.Invoke(_videoPro.Value);
  153. if (string.IsNullOrEmpty(newFayu)) newFayu = "00h:00m";
  154. CurrentFaYuTimeString = newFayu;
  155. }
  156. /// <summary>
  157. /// 转换为时分秒格式
  158. /// </summary>
  159. /// <param name="timespan"></param>
  160. /// <returns></returns>
  161. private string RevertToTime(double millisecond)
  162. {
  163. TimeSpan timespan = TimeSpan.FromMilliseconds(millisecond);
  164. return $"{(timespan.Days * 24 + timespan.Hours).ToString("D2")}:{timespan.Minutes.ToString("D2")}:{(timespan.Seconds + ((int)Math.Round(timespan.Milliseconds / 1000.00))).ToString("D2")}";
  165. }
  166. private bool IsUpdataFun(CustomProgressbar source)
  167. {
  168. if (source.IsLoaded && source.IsUpdata) return true;
  169. return false;
  170. }
  171. }
  172. }