RopeEquipmentControl.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using SWRIS.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Animation;
  9. namespace SWRIS.Controls
  10. {
  11. /// <summary>
  12. /// RopeControl.xaml 的交互逻辑
  13. /// </summary>
  14. public partial class RopeEquipmentControl : UserControl
  15. {
  16. private readonly Storyboard _animationStoryboard;
  17. public RopeEquipmentControl()
  18. {
  19. InitializeComponent();
  20. // 初始化动画
  21. _animationStoryboard = (Storyboard)Resources["RopeAnimation"];
  22. // 设置初始动画值
  23. UpdateAnimationValues();
  24. }
  25. #region 依赖属性
  26. // 绳索高度
  27. public static readonly DependencyProperty RopeHeightProperty =
  28. DependencyProperty.Register("RopeHeight", typeof(double), typeof(RopeEquipmentControl),
  29. new PropertyMetadata(900.0));
  30. // 绳索图片
  31. public static readonly DependencyProperty RopeImageSourceProperty =
  32. DependencyProperty.Register("RopeImageSource", typeof(ImageSource), typeof(RopeEquipmentControl));
  33. // 设备图片
  34. public static readonly DependencyProperty EquipmentImageSourceProperty =
  35. DependencyProperty.Register("EquipmentImageSource", typeof(ImageSource), typeof(RopeEquipmentControl));
  36. // 是否运行动画
  37. public static readonly DependencyProperty IsAnimationRunningProperty =
  38. DependencyProperty.Register("IsAnimationRunning", typeof(bool), typeof(RopeEquipmentControl),
  39. new PropertyMetadata(false, OnIsAnimationRunningChanged));
  40. // 动画方向(正向/反向)
  41. public static readonly DependencyProperty IsAnimationReversedProperty =
  42. DependencyProperty.Register("IsAnimationReversed", typeof(bool), typeof(RopeEquipmentControl),
  43. new PropertyMetadata(false, OnAnimationDirectionChanged));
  44. // 动画速度(秒)
  45. public static readonly DependencyProperty AnimationDurationProperty =
  46. DependencyProperty.Register("AnimationDuration", typeof(double), typeof(RopeEquipmentControl),
  47. new PropertyMetadata(0.5, OnAnimationDurationChanged));
  48. // 数据面板位置
  49. public static readonly DependencyProperty PanelMarginProperty =
  50. DependencyProperty.Register("PanelMargin", typeof(Thickness), typeof(RopeEquipmentControl));
  51. // 数据面板文字大小
  52. public static readonly DependencyProperty PanelNameFontSizeProperty =
  53. DependencyProperty.Register("PanelNameFontSize", typeof(double), typeof(RopeEquipmentControl),
  54. new PropertyMetadata(28d));
  55. // 数据面板文字大小
  56. public static readonly DependencyProperty PanelValueFontSizeProperty =
  57. DependencyProperty.Register("PanelValueFontSize", typeof(double), typeof(RopeEquipmentControl),
  58. new PropertyMetadata(28d));
  59. // 设备图片位置
  60. public static readonly DependencyProperty EquipmentTopPositionProperty =
  61. DependencyProperty.Register("EquipmentTopPosition", typeof(double), typeof(RopeEquipmentControl));
  62. #endregion
  63. #region 属性包装器
  64. public double RopeHeight
  65. {
  66. get => (double)GetValue(RopeHeightProperty);
  67. set => SetValue(RopeHeightProperty, value);
  68. }
  69. public ImageSource RopeImageSource
  70. {
  71. get => (ImageSource)GetValue(RopeImageSourceProperty);
  72. set => SetValue(RopeImageSourceProperty, value);
  73. }
  74. public ImageSource EquipmentImageSource
  75. {
  76. get => (ImageSource)GetValue(EquipmentImageSourceProperty);
  77. set => SetValue(EquipmentImageSourceProperty, value);
  78. }
  79. public bool IsAnimationRunning
  80. {
  81. get => (bool)GetValue(IsAnimationRunningProperty);
  82. set => SetValue(IsAnimationRunningProperty, value);
  83. }
  84. public bool IsAnimationReversed
  85. {
  86. get => (bool)GetValue(IsAnimationReversedProperty);
  87. set => SetValue(IsAnimationReversedProperty, value);
  88. }
  89. public double AnimationDuration
  90. {
  91. get => (double)GetValue(AnimationDurationProperty);
  92. set => SetValue(AnimationDurationProperty, value);
  93. }
  94. public Thickness PanelMargin
  95. {
  96. get => (Thickness)GetValue(PanelMarginProperty);
  97. set => SetValue(PanelMarginProperty, value);
  98. }
  99. public double PanelNameFontSize
  100. {
  101. get => (double)GetValue(PanelNameFontSizeProperty);
  102. set => SetValue(PanelNameFontSizeProperty, value);
  103. }
  104. public double PanelValueFontSize
  105. {
  106. get => (double)GetValue(PanelValueFontSizeProperty);
  107. set => SetValue(PanelValueFontSizeProperty, value);
  108. }
  109. public double EquipmentTopPosition
  110. {
  111. get => (double)GetValue(EquipmentTopPositionProperty);
  112. set => SetValue(EquipmentTopPositionProperty, value);
  113. }
  114. #endregion
  115. #region 动画控制
  116. private static void OnIsAnimationRunningChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  117. {
  118. var control = (RopeEquipmentControl)d;
  119. control.UpdateAnimationState();
  120. }
  121. private static void OnAnimationDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  122. {
  123. var control = (RopeEquipmentControl)d;
  124. control.UpdateAnimationValues();
  125. }
  126. private static void OnAnimationDurationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  127. {
  128. var control = (RopeEquipmentControl)d;
  129. control.UpdateAnimationValues();
  130. }
  131. private void UpdateAnimationValues()
  132. {
  133. var rectAnimation = (RectAnimation)_animationStoryboard.Children[0];
  134. rectAnimation.Duration = TimeSpan.FromSeconds(AnimationDuration);
  135. if (IsAnimationReversed)
  136. {
  137. rectAnimation.From = new Rect(0, -34, 40, 34);
  138. rectAnimation.To = new Rect(0, 0, 40, 34);
  139. }
  140. else
  141. {
  142. rectAnimation.From = new Rect(0, 0, 40, 34);
  143. rectAnimation.To = new Rect(0, -34, 40, 34);
  144. }
  145. if (IsAnimationRunning)
  146. {
  147. _animationStoryboard.Begin(this, true);
  148. }
  149. }
  150. private void UpdateAnimationState()
  151. {
  152. if (IsAnimationRunning)
  153. {
  154. _animationStoryboard.Begin(this, true);
  155. }
  156. else
  157. {
  158. _animationStoryboard.Stop(this);
  159. }
  160. }
  161. #endregion
  162. #region 公共方法
  163. public void StartAnimation(bool reversed = false)
  164. {
  165. IsAnimationReversed = reversed;
  166. IsAnimationRunning = true;
  167. }
  168. public void StopAnimation()
  169. {
  170. IsAnimationRunning = false;
  171. }
  172. public void ReverseAnimation()
  173. {
  174. IsAnimationReversed = !IsAnimationReversed;
  175. }
  176. #endregion
  177. }
  178. }