| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- namespace ivf_tl_Manage.UserControls
- {
- /// <summary>
- /// ProgressbarUserControl.xaml 的交互逻辑
- /// </summary>
- public partial class ProgressbarUserControl : UserControl
- {
- /// <summary>
- /// 进度条变化事件
- /// </summary>
- public event Action<double> ProChangendEvent;
- /// <summary>
- /// 操作之前发生的事件
- /// </summary>
- public event Action BeforeInteractionEvent;
- /// <summary>
- /// 操作进度条之后发生的事件
- /// </summary>
- public event Action<double> AfterInteractionEvent;
- /// <summary>
- /// 获取发育时长事件
- /// </summary>
- public event Func<double, string> LengthDevelopmentEvent;
- public ProgressbarUserControl()
- {
- InitializeComponent();
- this._videoPro.AddHandler(Slider.PreviewMouseLeftButtonUpEvent, new MouseButtonEventHandler(_videoPro_PreviewMouseLeftButtonUp), true);
- this._videoPro.AddHandler(Slider.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(_videoPro_PreviewMouseLeftButtonDown), true);
- }
- public bool IsUpdata
- {
- get { return (bool)GetValue(IsUpdataProperty); }
- set { SetValue(IsUpdataProperty, value); }
- }
- // Using a DependencyProperty as the backing store for IsUpdata. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty IsUpdataProperty =
- DependencyProperty.Register("IsUpdata", typeof(bool), typeof(ProgressbarUserControl), new PropertyMetadata(false));
- public string CurrentFaYuTimeString
- {
- get { return (string)GetValue(CurrentFaYuTimeStringProperty); }
- set { SetValue(CurrentFaYuTimeStringProperty, value); }
- }
- // Using a DependencyProperty as the backing store for CurrentFaYuTimeString. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty CurrentFaYuTimeStringProperty =
- DependencyProperty.Register("CurrentFaYuTimeString", typeof(string), typeof(ProgressbarUserControl), new PropertyMetadata("00h:00m"));
- public double VideoAllTime
- {
- get { return (double)GetValue(VideoAllTimeProperty); }
- set { SetValue(VideoAllTimeProperty, value); }
- }
- // Using a DependencyProperty as the backing store for VideoAllTime. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty VideoAllTimeProperty =
- DependencyProperty.Register("VideoAllTime", typeof(double), typeof(ProgressbarUserControl), new PropertyMetadata(1d));
- public double VideoCurrentTime
- {
- get { return (double)GetValue(VideoCurrentTimeProperty); }
- set { SetValue(VideoCurrentTimeProperty, value); }
- }
- // Using a DependencyProperty as the backing store for VideoCurrentTime. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty VideoCurrentTimeProperty =
- DependencyProperty.Register("VideoCurrentTime", typeof(double), typeof(ProgressbarUserControl), new PropertyMetadata(0d, new PropertyChangedCallback(VideoCurrentTimePropertyChangedCallback)));
- private static void VideoCurrentTimePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (!(d is ProgressbarUserControl source)) return;
- if (!source.IsUpdataFun(source)) return;
- if (source.isInChange) return;
- if (!(e.NewValue is double newTime)) return;
- source._PlayTime.Text = source.RevertToTime(newTime);
- source._AllTime.Text = source.RevertToTime(source.VideoAllTime - newTime);
- source._videoPro.Value = newTime;
- }
- /// <summary>
- /// 进度条操作开始
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- /// <exception cref="NotImplementedException"></exception>
- private void _videoPro_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (!IsUpdataFun(this)) return;
- BeforeInteractionEvent?.Invoke();
- }
- /// <summary>
- /// 进度条操作结束
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- /// <exception cref="NotImplementedException"></exception>
- private void _videoPro_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (!IsUpdataFun(this)) return;
- AfterInteractionEvent?.Invoke(this._videoPro.Value);
- }
- /// <summary>
- /// 开始拖动进度条
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void _videoPro_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
- {
- if (!IsUpdataFun(this)) return;
- BeforeInteractionEvent?.Invoke();
- }
- /// <summary>
- /// 拖动进度条
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void _videoPro_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
- {
- if (!IsUpdataFun(this)) return;
- ProChangendEvent?.Invoke(this._videoPro.Value);
- }
- bool isInChange = false;
- double newTime = 0d;
- string newFayu = "00h:00m";
- /// <summary>
- /// 进度条改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void _videoPro_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- if (!IsUpdataFun(this)) return;
- newTime = this._videoPro.Value;
- this._PlayTime.Text = RevertToTime(newTime);
- this._AllTime.Text = RevertToTime(VideoAllTime - newTime);
- isInChange = true;
- VideoCurrentTime = newTime;
- isInChange = false;
- newFayu = LengthDevelopmentEvent?.Invoke(_videoPro.Value);
- if (string.IsNullOrEmpty(newFayu)) newFayu = "00h:00m";
- CurrentFaYuTimeString = newFayu;
- }
- /// <summary>
- /// 转换为时分秒格式
- /// </summary>
- /// <param name="timespan"></param>
- /// <returns></returns>
- private string RevertToTime(double millisecond)
- {
- TimeSpan timespan = TimeSpan.FromMilliseconds(millisecond);
- return $"{(timespan.Days * 24 + timespan.Hours).ToString("D2")}:{timespan.Minutes.ToString("D2")}:{(timespan.Seconds + ((int)Math.Round(timespan.Milliseconds / 1000.00))).ToString("D2")}";
- }
- private bool IsUpdataFun(ProgressbarUserControl source)
- {
- if (source.IsLoaded && source.IsUpdata) return true;
- return false;
- }
- }
- }
|