| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- using SWRIS.Models;
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- namespace SWRIS.Controls
- {
- /// <summary>
- /// RopeControl.xaml 的交互逻辑
- /// </summary>
- public partial class RopeEquipmentControl : UserControl
- {
- private readonly Storyboard _animationStoryboard;
- public RopeEquipmentControl()
- {
- InitializeComponent();
- // 初始化动画
- _animationStoryboard = (Storyboard)Resources["RopeAnimation"];
- // 设置初始动画值
- UpdateAnimationValues();
- }
- #region 依赖属性
- // 绳索高度
- public static readonly DependencyProperty RopeHeightProperty =
- DependencyProperty.Register("RopeHeight", typeof(double), typeof(RopeEquipmentControl),
- new PropertyMetadata(900.0));
- // 绳索图片
- public static readonly DependencyProperty RopeImageSourceProperty =
- DependencyProperty.Register("RopeImageSource", typeof(ImageSource), typeof(RopeEquipmentControl));
- // 设备图片
- public static readonly DependencyProperty EquipmentImageSourceProperty =
- DependencyProperty.Register("EquipmentImageSource", typeof(ImageSource), typeof(RopeEquipmentControl));
- // 是否运行动画
- public static readonly DependencyProperty IsAnimationRunningProperty =
- DependencyProperty.Register("IsAnimationRunning", typeof(bool), typeof(RopeEquipmentControl),
- new PropertyMetadata(false, OnIsAnimationRunningChanged));
- // 动画方向(正向/反向)
- public static readonly DependencyProperty IsAnimationReversedProperty =
- DependencyProperty.Register("IsAnimationReversed", typeof(bool), typeof(RopeEquipmentControl),
- new PropertyMetadata(false, OnAnimationDirectionChanged));
- // 动画速度(秒)
- public static readonly DependencyProperty AnimationDurationProperty =
- DependencyProperty.Register("AnimationDuration", typeof(double), typeof(RopeEquipmentControl),
- new PropertyMetadata(0.5, OnAnimationDurationChanged));
- // 数据面板位置
- public static readonly DependencyProperty PanelMarginProperty =
- DependencyProperty.Register("PanelMargin", typeof(Thickness), typeof(RopeEquipmentControl));
- // 数据面板文字大小
- public static readonly DependencyProperty PanelNameFontSizeProperty =
- DependencyProperty.Register("PanelNameFontSize", typeof(double), typeof(RopeEquipmentControl),
- new PropertyMetadata(28d));
- // 数据面板文字大小
- public static readonly DependencyProperty PanelValueFontSizeProperty =
- DependencyProperty.Register("PanelValueFontSize", typeof(double), typeof(RopeEquipmentControl),
- new PropertyMetadata(28d));
- // 设备图片位置
- public static readonly DependencyProperty EquipmentTopPositionProperty =
- DependencyProperty.Register("EquipmentTopPosition", typeof(double), typeof(RopeEquipmentControl));
- #endregion
- #region 属性包装器
- public double RopeHeight
- {
- get => (double)GetValue(RopeHeightProperty);
- set => SetValue(RopeHeightProperty, value);
- }
- public ImageSource RopeImageSource
- {
- get => (ImageSource)GetValue(RopeImageSourceProperty);
- set => SetValue(RopeImageSourceProperty, value);
- }
- public ImageSource EquipmentImageSource
- {
- get => (ImageSource)GetValue(EquipmentImageSourceProperty);
- set => SetValue(EquipmentImageSourceProperty, value);
- }
- public bool IsAnimationRunning
- {
- get => (bool)GetValue(IsAnimationRunningProperty);
- set => SetValue(IsAnimationRunningProperty, value);
- }
- public bool IsAnimationReversed
- {
- get => (bool)GetValue(IsAnimationReversedProperty);
- set => SetValue(IsAnimationReversedProperty, value);
- }
- public double AnimationDuration
- {
- get => (double)GetValue(AnimationDurationProperty);
- set => SetValue(AnimationDurationProperty, value);
- }
- public Thickness PanelMargin
- {
- get => (Thickness)GetValue(PanelMarginProperty);
- set => SetValue(PanelMarginProperty, value);
- }
- public double PanelNameFontSize
- {
- get => (double)GetValue(PanelNameFontSizeProperty);
- set => SetValue(PanelNameFontSizeProperty, value);
- }
- public double PanelValueFontSize
- {
- get => (double)GetValue(PanelValueFontSizeProperty);
- set => SetValue(PanelValueFontSizeProperty, value);
- }
- public double EquipmentTopPosition
- {
- get => (double)GetValue(EquipmentTopPositionProperty);
- set => SetValue(EquipmentTopPositionProperty, value);
- }
- #endregion
- #region 动画控制
- private static void OnIsAnimationRunningChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var control = (RopeEquipmentControl)d;
- control.UpdateAnimationState();
- }
- private static void OnAnimationDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var control = (RopeEquipmentControl)d;
- control.UpdateAnimationValues();
- }
- private static void OnAnimationDurationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var control = (RopeEquipmentControl)d;
- control.UpdateAnimationValues();
- }
- private void UpdateAnimationValues()
- {
- var rectAnimation = (RectAnimation)_animationStoryboard.Children[0];
- rectAnimation.Duration = TimeSpan.FromSeconds(AnimationDuration);
- if (IsAnimationReversed)
- {
- rectAnimation.From = new Rect(0, -34, 40, 34);
- rectAnimation.To = new Rect(0, 0, 40, 34);
- }
- else
- {
- rectAnimation.From = new Rect(0, 0, 40, 34);
- rectAnimation.To = new Rect(0, -34, 40, 34);
- }
- if (IsAnimationRunning)
- {
- _animationStoryboard.Begin(this, true);
- }
- }
- private void UpdateAnimationState()
- {
- if (IsAnimationRunning)
- {
- _animationStoryboard.Begin(this, true);
- }
- else
- {
- _animationStoryboard.Stop(this);
- }
- }
- #endregion
- #region 公共方法
- public void StartAnimation(bool reversed = false)
- {
- IsAnimationReversed = reversed;
- IsAnimationRunning = true;
- }
- public void StopAnimation()
- {
- IsAnimationRunning = false;
- }
- public void ReverseAnimation()
- {
- IsAnimationReversed = !IsAnimationReversed;
- }
- #endregion
- }
- }
|