终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

221 linhas
8.2 KiB

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