using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ivf_tl_Operate.CustomUserControls
{
///
/// CustomProgressbar.xaml 的交互逻辑
///
public partial class CustomProgressbar : UserControl
{
///
/// 进度条变化事件
///
public event Action ProChangendEvent;
///
/// 操作之前发生的事件
///
public event Action BeforeInteractionEvent;
///
/// 操作进度条之后发生的事件
///
public event Action AfterInteractionEvent;
///
/// 获取发育时长事件
///
public event Func LengthDevelopmentEvent;
public CustomProgressbar()
{
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(CustomProgressbar), 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(CustomProgressbar), 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(CustomProgressbar), 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(CustomProgressbar), new PropertyMetadata(0d, new PropertyChangedCallback(VideoCurrentTimePropertyChangedCallback)));
private static void VideoCurrentTimePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is CustomProgressbar 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;
}
///
/// 进度条操作开始
///
///
///
///
private void _videoPro_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!IsUpdataFun(this)) return;
BeforeInteractionEvent?.Invoke();
}
///
/// 进度条操作结束
///
///
///
///
private void _videoPro_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (!IsUpdataFun(this)) return;
AfterInteractionEvent?.Invoke(this._videoPro.Value);
}
///
/// 开始拖动进度条
///
///
///
private void _videoPro_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
if (!IsUpdataFun(this)) return;
BeforeInteractionEvent?.Invoke();
}
///
/// 拖动进度条
///
///
///
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";
///
/// 进度条改变
///
///
///
private void _videoPro_ValueChanged(object sender, RoutedPropertyChangedEventArgs 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;
}
///
/// 转换为时分秒格式
///
///
///
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(CustomProgressbar source)
{
if (source.IsLoaded && source.IsUpdata) return true;
return false;
}
}
}