终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

193 lines
6.9 KiB

  1. using BPASmart.Model;
  2. using BPASmartClient.Compiler;
  3. using BPASmartClient.DATABUS;
  4. using BPASmartClient.SCADAControl;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  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.Imaging;
  20. using System.Windows.Navigation;
  21. using System.Windows.Shapes;
  22. using System.Windows.Threading;
  23. namespace BPASmartClient.SCADAControl.CustomerControls
  24. {
  25. public class TheTextBox : TextBox, IExecutable
  26. {
  27. static TheTextBox()
  28. {
  29. DefaultStyleKeyProperty.OverrideMetadata(typeof(TheTextBox), new FrameworkPropertyMetadata(typeof(TheTextBox)));
  30. }
  31. public TheTextBox()
  32. {
  33. ResourceDictionary languageResDic = new ResourceDictionary();
  34. languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute);
  35. this.Resources.MergedDictionaries.Add(languageResDic);
  36. Height = 30;
  37. Width = 80;
  38. FontSize = 16;
  39. this.KeyDown += TheTextBox_KeyDown;
  40. this.TextChanged += TheTextBox_TextChanged;
  41. }
  42. bool isRun=false;
  43. private void TheTextBox_KeyDown(object sender,KeyEventArgs e)
  44. {
  45. isRun = true;
  46. }
  47. private void TheTextBox_TextChanged(object sender,TextChangedEventArgs e)
  48. {
  49. if (isRun)
  50. {
  51. timer.Stop();
  52. foreach (var item in propertyBing)
  53. {
  54. //{Binding 测试设备.VAR_A_2}
  55. string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split(".");
  56. if (str.Length > 1)
  57. {
  58. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0]))
  59. {
  60. Dictionary<string,DeviceDataModel> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
  61. if (b != null && b.ContainsKey(str[1]))
  62. {
  63. object _value = b[str[1]].VarVaule;
  64. EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType);
  65. SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = Text,DataType = eDataType });
  66. }
  67. }
  68. }
  69. }
  70. Config.GetInstance().RunJsScipt(ValueChangedExecute);
  71. timer.Start();
  72. isRun=false;
  73. }
  74. }
  75. public string ControlType => "控件";
  76. private bool isExecuteState;
  77. public bool IsExecuteState
  78. {
  79. get { return isExecuteState; }
  80. set
  81. {
  82. isExecuteState = value;
  83. if (IsExecuteState)
  84. {
  85. IsEnabled = true;
  86. Focusable = true;
  87. Register();
  88. //Style = null;
  89. }
  90. }
  91. }
  92. #region 数据绑定模块
  93. public string ValueChangedExecute
  94. {
  95. get { return (string)GetValue(ValueChangedExecuteProperty); }
  96. set { SetValue(ValueChangedExecuteProperty,value); }
  97. }
  98. public static readonly DependencyProperty ValueChangedExecuteProperty =
  99. DependencyProperty.Register("ValueChangedExecute",typeof(string),typeof(TheTextBox),new PropertyMetadata(string.Empty));
  100. [Category("事件")]
  101. public string SendText
  102. {
  103. get { return (string)GetValue(SendTextProperty); }
  104. set { SetValue(SendTextProperty, value); }
  105. }
  106. public static readonly DependencyProperty SendTextProperty =
  107. DependencyProperty.Register("SendText", typeof(string),typeof(TheTextBox),new PropertyMetadata(string.Empty));
  108. [Category("数据绑定-数据来源")]
  109. public int TimeCount
  110. {
  111. get { return (int)GetValue(TimeCountProperty); }
  112. set { SetValue(TimeCountProperty,value); }
  113. }
  114. public static readonly DependencyProperty TimeCountProperty =
  115. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheTextBox),new PropertyMetadata(100));
  116. public event EventHandler PropertyChange; //声明一个事件
  117. /// <summary>
  118. /// 属性刷新器
  119. /// </summary>
  120. DispatcherTimer timer = new DispatcherTimer();
  121. /// <summary>
  122. /// 属性绑定变量集合
  123. /// </summary>
  124. Dictionary<string,string> propertyBing = new Dictionary<string,string>();
  125. /// <summary>
  126. /// 运行事件
  127. /// </summary>
  128. public void Register()
  129. {
  130. PropertyInfo[] propertyInfos = this.GetType().GetProperties();
  131. foreach (PropertyInfo propertyInfo in propertyInfos)
  132. {
  133. var propName = propertyInfo?.GetValue(this,null);
  134. if (propName is string && propName != null && propName.ToString().Contains("Binding ") && propName.ToString().Contains("."))
  135. {
  136. propertyBing[propertyInfo.Name] = propName.ToString();
  137. }
  138. }
  139. if (propertyBing.Count > 0)
  140. {
  141. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  142. timer.Tick += Timer_Tick; ;
  143. timer.Start();
  144. }
  145. }
  146. /// <summary>
  147. /// 属性刷新事件
  148. /// </summary>
  149. /// <param name="sender"></param>
  150. /// <param name="e"></param>
  151. private void Timer_Tick(object? sender,EventArgs e)
  152. {
  153. try
  154. {
  155. foreach (var item in propertyBing)
  156. {
  157. //{Binding 测试设备.VAR_A_2}
  158. string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split(".");
  159. if (str.Length > 1)
  160. {
  161. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0]))
  162. {
  163. Dictionary<string,DeviceDataModel> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
  164. if (b != null && b.ContainsKey(str[1]))
  165. {
  166. object _value = b[str[1]].VarVaule;
  167. this.GetType().GetProperty(item.Key).SetValue(this,_value);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. }
  176. }
  177. #endregion
  178. }
  179. }