终端一体化运控平台
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.
 
 
 

134 lines
4.5 KiB

  1. using BPASmartClient.Compiler;
  2. using BPASmartClient.DATABUS;
  3. using BPASmartClient.SCADAControl;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. using System.Windows.Threading;
  21. namespace BPASmartClient.SCADAControl.CustomerControls
  22. {
  23. public class TheTextBlock :TextBlock, IExecutable
  24. {
  25. static TheTextBlock()
  26. {
  27. DefaultStyleKeyProperty.OverrideMetadata(typeof(TheTextBlock),new FrameworkPropertyMetadata(typeof(TheTextBlock)));
  28. }
  29. public TheTextBlock()
  30. {
  31. ResourceDictionary languageResDic = new ResourceDictionary();
  32. languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute);
  33. this.Resources.MergedDictionaries.Add(languageResDic);
  34. Height = 30;
  35. Width = 80;
  36. }
  37. public string ControlType => "控件";
  38. private bool isExecuteState;
  39. public bool IsExecuteState
  40. {
  41. get { return isExecuteState; }
  42. set
  43. {
  44. isExecuteState = value;
  45. if (IsExecuteState)
  46. {
  47. Register();
  48. }
  49. }
  50. }
  51. #region 数据绑定模块
  52. [Category("数据绑定-数据来源")]
  53. public int TimeCount
  54. {
  55. get { return (int)GetValue(TimeCountProperty); }
  56. set { SetValue(TimeCountProperty,value); }
  57. }
  58. public static readonly DependencyProperty TimeCountProperty =
  59. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheTextBlock),new PropertyMetadata(50));
  60. public event EventHandler PropertyChange; //声明一个事件
  61. /// <summary>
  62. /// 属性刷新器
  63. /// </summary>
  64. DispatcherTimer timer = new DispatcherTimer();
  65. /// <summary>
  66. /// 属性绑定变量集合
  67. /// </summary>
  68. Dictionary<string,string> propertyBing = new Dictionary<string,string>();
  69. /// <summary>
  70. /// 运行事件
  71. /// </summary>
  72. public void Register()
  73. {
  74. PropertyInfo[] propertyInfos = this.GetType().GetProperties();
  75. foreach (PropertyInfo propertyInfo in propertyInfos)
  76. {
  77. var propName = propertyInfo?.GetValue(this,null);
  78. if (propName is string && propName != null && propName.ToString().Contains("Binding ") && propName.ToString().Contains("."))
  79. {
  80. string va = string.Empty;
  81. if (propName.ToString().StartsWith("{}")) va = propName.ToString().Replace("{}","");
  82. else va = propName.ToString();
  83. propertyBing[propertyInfo.Name] = va;
  84. }
  85. }
  86. if (propertyBing.Count > 0)
  87. {
  88. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  89. timer.Tick += Timer_Tick; ;
  90. timer.Start();
  91. }
  92. }
  93. /// <summary>
  94. /// 属性刷新事件
  95. /// </summary>
  96. /// <param name="sender"></param>
  97. /// <param name="e"></param>
  98. private void Timer_Tick(object? sender,EventArgs e)
  99. {
  100. try
  101. {
  102. foreach (var item in propertyBing)
  103. {
  104. //{Binding 测试设备.VAR_A_2}
  105. string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split(".");
  106. if (str.Length > 1)
  107. {
  108. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0]))
  109. {
  110. Dictionary<string,DeviceDataModel> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
  111. if (b != null && b.ContainsKey(str[1]))
  112. {
  113. object _value = b[str[1]].VarVaule;
  114. this.GetType().GetProperty(item.Key).SetValue(this,_value);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. }
  123. }
  124. #endregion
  125. }
  126. }