ProgressbarUserControl.xaml.cs 7.2 KB

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