|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- 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
- {
- /// <summary>
- /// PipeLineTL.xaml 的交互逻辑
- /// </summary>
- public partial class PipeLineTL : UserControl
- {
- public PipeLineTL()
- {
- InitializeComponent();
- this.Loaded += (s, e) => { Play(); Refresh(); };
- }
-
- private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- (d as PipeLineTL)?.Refresh();
- }
-
- private void Refresh()
- {
- this.mp.StrokeThickness = LineWidth;
- Canvas.SetRight(this.mp, 25 - LineWidth / 2);
- Canvas.SetBottom(this.mp, 25 - LineWidth / 2);
- this.mp.Stroke = LineBrush;
- //this.Width = PipeWidth * 2;
- //this.Height = PipeWidth * 2;
- }
-
- #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 * 3.14 / 4;
- else if (Direction == 1) da.To = this.ActualWidth * 3.14 / 4 * -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 PipeLineTL)?.Animation();
- }
-
- private void Animation()
- {
- if (Direction == 0)
- {
- sb.Stop();
- this.mp.Visibility = Visibility.Collapsed;
- }
- else Play();
- }
-
- /// <summary>
- /// 液体流向控制
- /// 0:停止流动,1正向流动,2反向流动
- /// </summary>
- public int Direction
- {
- get { return (int)GetValue(DirectionProperty); }
- set { SetValue(DirectionProperty, value); }
- }
- public static readonly DependencyProperty DirectionProperty =
- DependencyProperty.Register("Direction", typeof(int), typeof(PipeLineTL), new(0, new(OnAnimationChanged)));
-
- /// <summary>
- /// 液体流动速度
- /// 液体流动一次的时间,单位秒
- /// </summary>
- public float Speed
- {
- get { return (float)GetValue(SpeedProperty); }
- set { SetValue(SpeedProperty, value); }
- }
- public static readonly DependencyProperty SpeedProperty =
- DependencyProperty.Register("Speed", typeof(float), typeof(PipeLineTL), new(8.0f, new(OnAnimationChanged)));
- #endregion
-
-
- /// <summary>
- /// 流动宽度
- /// </summary>
- public int LineWidth
- {
- get { return (int)GetValue(LineWidthProperty); }
- set { SetValue(LineWidthProperty, value); }
- }
- public static readonly DependencyProperty LineWidthProperty =
- DependencyProperty.Register("LineWidth", typeof(int), typeof(PipeLineTL), new(4, new(OnPropertyChanged)));
-
-
- /// <summary>
- /// 流动线条颜色
- /// </summary>
- public Brush LineBrush
- {
- get { return (Brush)GetValue(LineBrushProperty); }
- set { SetValue(LineBrushProperty, value); }
- }
- public static readonly DependencyProperty LineBrushProperty =
- DependencyProperty.Register("LineBrush", typeof(Brush), typeof(PipeLineTL), new(Brushes.DeepSkyBlue));
-
- public int PipeWidth
- {
- get { return (int)GetValue(PipeWidthProperty); }
- set { SetValue(PipeWidthProperty, value); }
- }
- public static readonly DependencyProperty PipeWidthProperty =
- DependencyProperty.Register("PipeWidth", typeof(int), typeof(PipeLineTL), new(30, new(OnPropertyChanged)));
-
- /// <summary>
- /// 管道内壁颜色
- /// </summary>
- public Color Fill
- {
- get { return (Color)GetValue(FillProperty); }
- set { SetValue(FillProperty, value); }
- }
- public static readonly DependencyProperty FillProperty =
- DependencyProperty.Register("Fill", typeof(Color), typeof(PipeLineTL), new(Brushes.DimGray.Color));
- }
- }
|