using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; 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 BPASmartClient.CustomResource.UserControls { /// /// PipeLineH.xaml 的交互逻辑 /// public partial class PipeLineH : UserControl { public PipeLineH() { InitializeComponent(); this.SizeChanged += PipeLineH_SizeChanged; this.Loaded += (s, e) => { Play(); Refresh(); }; } private void PipeLineH_SizeChanged(object sender, SizeChangedEventArgs e) { LineChange(); } private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as PipeLineH)?.Refresh(); } private void LineChange() { PathGeometry geometry1 = new PathGeometry(); PathFigure pathFigure1 = new PathFigure(); pathFigure1.StartPoint = new Point(0, this.ActualHeight / 2); pathFigure1.Segments.Add(new LineSegment(new Point(this.ActualWidth, this.ActualHeight / 2), true)); geometry1.Figures.Add(pathFigure1); this.mp.Data = geometry1; } private void Refresh() { LineChange(); this.mp.StrokeThickness = LineWidth; this.mp.Stroke = LineBrush; } #region 动画相关 Storyboard sb = new Storyboard(); private void Play() { sb.Children.Clear(); DoubleAnimation da = new DoubleAnimation(); da.From = 0; if (Direction == 2) da.To = this.ActualWidth; else if (Direction == 1) da.To = this.ActualWidth * -1; da.RepeatBehavior = RepeatBehavior.Forever;//设置无限循环播放 da.Duration = TimeSpan.FromMilliseconds(Speed * 1000);//设置动画时间 //da.AutoReverse = true;//设置可以反转播放动画 Storyboard.SetTarget(da, mp);//绑定动画 Storyboard.SetTargetProperty(da, new PropertyPath("StrokeDashOffset")); sb.Children.Add(da);//添加动画 if(Direction!=0) this.mp.Visibility = Visibility.Visible; sb.Begin();//播放动画 } private static void OnAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as PipeLineH)?.Animation(); } private void Animation() { if (Direction == 0) { sb.Stop(); this.mp.Visibility = Visibility.Collapsed; } else Play(); } /// /// 液体流向控制 /// 0:停止流动,1正向流动,2反向流动 /// public int Direction { get { return (int)GetValue(DirectionProperty); } set { SetValue(DirectionProperty, value); } } public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register("Direction", typeof(int), typeof(PipeLineH), new(0, new(OnAnimationChanged))); /// /// 液体流动速度 /// 液体流动一次的时间,单位秒 /// public float Speed { get { return (float)GetValue(SpeedProperty); } set { SetValue(SpeedProperty, value); } } public static readonly DependencyProperty SpeedProperty = DependencyProperty.Register("Speed", typeof(float), typeof(PipeLineH), new(8.0f, new(OnAnimationChanged))); #endregion /// /// 流动宽度 /// public int LineWidth { get { return (int)GetValue(LineWidthProperty); } set { SetValue(LineWidthProperty, value); } } public static readonly DependencyProperty LineWidthProperty = DependencyProperty.Register("LineWidth", typeof(int), typeof(PipeLineH), new(4, new(OnPropertyChanged))); /// /// 流动线条颜色 /// public Brush LineBrush { get { return (Brush)GetValue(LineBrushProperty); } set { SetValue(LineBrushProperty, value); } } public static readonly DependencyProperty LineBrushProperty = DependencyProperty.Register("LineBrush", typeof(Brush), typeof(PipeLineH), new(Brushes.DeepSkyBlue)); /// /// 管道内壁颜色 /// public Color Fill { get { return (Color)GetValue(FillProperty); } set { SetValue(FillProperty, value); } } public static readonly DependencyProperty FillProperty = DependencyProperty.Register("Fill", typeof(Color), typeof(PipeLineH), new(Brushes.DimGray.Color)); } }