终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

209 lignes
7.9 KiB

  1. using BPASmartClient.Compiler;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Windows;
  7. using System.Windows.Controls.Primitives;
  8. using System.Windows.Data;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. namespace BPASmartClient.SCADAControl.CustomerControls
  13. {
  14. /// <summary>
  15. /// 开关button
  16. /// </summary>
  17. [TemplatePart(Name = ELLIPSE, Type = typeof(FrameworkElement))]
  18. [TemplatePart(Name = TranslateX, Type = typeof(TranslateTransform))]
  19. public class SwitchButton : ToggleButton, IExecutable
  20. {
  21. public event EventHandler PropertyChange; //声明一个事件
  22. public SwitchButton()
  23. {
  24. ResourceDictionary languageResDic = new ResourceDictionary();
  25. languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute);
  26. this.Resources.MergedDictionaries.Add(languageResDic);
  27. SetCurrentValue(WidthProperty, 120d);
  28. SetCurrentValue(HeightProperty, 40d);
  29. }
  30. public const string ELLIPSE = "ELLIPSE";
  31. public const string TranslateX = "TranslateX";
  32. static SwitchButton()
  33. {
  34. DefaultStyleKeyProperty.OverrideMetadata(typeof(SwitchButton), new FrameworkPropertyMetadata(typeof(SwitchButton)));
  35. }
  36. [Category("事件")]
  37. public string SendText
  38. {
  39. get { return (string)GetValue(SendTextProperty); }
  40. set { SetValue(SendTextProperty, value); }
  41. }
  42. public static readonly DependencyProperty SendTextProperty =
  43. DependencyProperty.Register("SendText", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
  44. /// <summary>
  45. /// 不勾选时执行代码
  46. /// </summary>
  47. [Category("事件")]
  48. public string UnCheckedExec
  49. {
  50. get { return (string)GetValue(UnCheckedExecProperty); }
  51. set { SetValue(UnCheckedExecProperty, value); }
  52. }
  53. public static readonly DependencyProperty UnCheckedExecProperty =
  54. DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
  55. /// <summary>
  56. /// 勾选时执行代码
  57. /// </summary>
  58. [Category("事件")]
  59. public string CheckedExec
  60. {
  61. get { return (string)GetValue(CheckedExecProperty); }
  62. set { SetValue(CheckedExecProperty, value); }
  63. }
  64. public static readonly DependencyProperty CheckedExecProperty =
  65. DependencyProperty.Register("CheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
  66. protected override void OnRender(DrawingContext drawingContext)
  67. {
  68. base.OnRender(drawingContext);
  69. ellipse.Width = Height - 8;
  70. ellipse.Height = Height - 8;
  71. Refresh();
  72. }
  73. /// <summary>
  74. /// 数据模板
  75. /// </summary>
  76. private Dictionary<string, object> DataModel
  77. {
  78. get { return (Dictionary<string, object>)GetValue(DataModelProperty); }
  79. set { SetValue(DataModelProperty, value); }
  80. }
  81. private static readonly DependencyProperty DataModelProperty =
  82. DependencyProperty.Register("DataModel", typeof(Dictionary<string, object>), typeof(SwitchButton), new PropertyMetadata(new Dictionary<string, object>()));
  83. public Brush DKColor
  84. {
  85. get { return (Brush)GetValue(DKColorProperty); }
  86. set { SetValue(DKColorProperty, value); }
  87. }
  88. public static readonly DependencyProperty DKColorProperty =
  89. DependencyProperty.Register("DKColor", typeof(Brush), typeof(SwitchButton), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
  90. TranslateTransform transX;
  91. public Ellipse ellipse;
  92. public override void OnApplyTemplate()
  93. {
  94. base.OnApplyTemplate();
  95. ellipse = GetTemplateChild(ELLIPSE) as Ellipse;
  96. transX = GetTemplateChild(TranslateX) as TranslateTransform;
  97. }
  98. protected override void OnChecked(RoutedEventArgs e)
  99. {
  100. base.OnChecked(e);
  101. Refresh();
  102. }
  103. protected override void OnUnchecked(RoutedEventArgs e)
  104. {
  105. base.OnUnchecked(e);
  106. Refresh();
  107. }
  108. void Refresh()
  109. {
  110. if (ellipse == null)
  111. {
  112. return;
  113. }
  114. DoubleAnimation da = new DoubleAnimation();
  115. da.Duration = new Duration(TimeSpan.FromMilliseconds(250));
  116. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  117. if (IsChecked == true)
  118. {
  119. da.To = ActualWidth - ellipse.ActualWidth - 5;
  120. ellipse.SetCurrentValue(Ellipse.FillProperty, DKColor);
  121. }
  122. else
  123. {
  124. da.To = 3;
  125. ellipse.SetCurrentValue(Ellipse.FillProperty, Foreground);
  126. }
  127. transX.BeginAnimation(TranslateTransform.XProperty, da);
  128. }
  129. private bool isExecuteState;
  130. public bool IsExecuteState
  131. {
  132. get { return isExecuteState; }
  133. set
  134. {
  135. isExecuteState = value;
  136. if (IsExecuteState)
  137. {
  138. Register();
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// 运行
  144. /// </summary>
  145. public void Register()
  146. {
  147. Class_DataBus.GetInstance().BindingAction += BindingActionHeader;
  148. this.Click += SwitchButton_Click;
  149. if (this.IsChecked == true) Config.GetInstance().RunJsScipt(CheckedExec);
  150. else Config.GetInstance().RunJsScipt(UnCheckedExec);
  151. Refresh();
  152. }
  153. public void BindingActionHeader(object sender, EventArgs e)
  154. {
  155. this.Dispatcher.Invoke((Action)(() =>
  156. {
  157. DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding;
  158. PropertyChange?.Invoke(this, EventArgs.Empty);
  159. }));
  160. }
  161. private void SwitchButton_Click(object sender, RoutedEventArgs e)
  162. {
  163. Binding binding = BindingOperations.GetBinding(this, IsCheckedProperty);
  164. if (binding != null && !string.IsNullOrEmpty(binding.Path.Path))
  165. {
  166. string path = binding.Path.Path; string _Name = string.Empty; string _Value = string.Empty;
  167. if (!string.IsNullOrEmpty(path) && path.Contains("."))
  168. {
  169. try
  170. {
  171. _Name = path.Split('.')[0].Replace("DataModel[", "").TrimEnd(']');
  172. _Value = path.Split('.')[1];
  173. }
  174. catch (Exception ex)
  175. {
  176. }
  177. }
  178. Dictionary<string, string> blx = new Dictionary<string, string>();
  179. if (Class_DataBus.GetInstance().Dic_RedisDataType.ContainsKey(_Name))
  180. blx = Class_DataBus.GetInstance().Dic_RedisDataType[_Name];
  181. if (blx != null && blx.ContainsKey(_Value))
  182. {
  183. SendText = JsonConvert.SerializeObject(new PublishModel
  184. {
  185. DeviceName = _Name,
  186. VarName = _Value,
  187. Value = this.IsChecked.ToString(),
  188. DataType = (EDataType)Enum.Parse(typeof(EDataType), blx[_Value])
  189. });
  190. }
  191. }
  192. if (this.IsChecked == true) Config.GetInstance().RunJsScipt(CheckedExec);
  193. else Config.GetInstance().RunJsScipt(UnCheckedExec);
  194. }
  195. public string ControlType => "控件";
  196. }
  197. }