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

129 lines
4.4 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. }
  35. public string ControlType => "控件";
  36. private bool isExecuteState;
  37. public bool IsExecuteState
  38. {
  39. get { return isExecuteState; }
  40. set
  41. {
  42. isExecuteState = value;
  43. if (IsExecuteState)
  44. {
  45. Register();
  46. }
  47. }
  48. }
  49. #region 数据绑定模块
  50. [Category("数据绑定-数据来源")]
  51. public int TimeCount
  52. {
  53. get { return (int)GetValue(TimeCountProperty); }
  54. set { SetValue(TimeCountProperty,value); }
  55. }
  56. public static readonly DependencyProperty TimeCountProperty =
  57. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheTextBlock),new PropertyMetadata(5));
  58. public event EventHandler PropertyChange; //声明一个事件
  59. /// <summary>
  60. /// 属性刷新器
  61. /// </summary>
  62. DispatcherTimer timer = new DispatcherTimer();
  63. /// <summary>
  64. /// 属性绑定变量集合
  65. /// </summary>
  66. Dictionary<string,string> propertyBing = new Dictionary<string,string>();
  67. /// <summary>
  68. /// 运行事件
  69. /// </summary>
  70. public void Register()
  71. {
  72. PropertyInfo[] propertyInfos = this.GetType().GetProperties();
  73. foreach (PropertyInfo propertyInfo in propertyInfos)
  74. {
  75. var propName = propertyInfo?.GetValue(this,null);
  76. if (propName is string && propName != null && propName.ToString().Contains("Binding ") && propName.ToString().Contains("."))
  77. {
  78. string va = string.Empty;
  79. if (propName.ToString().StartsWith("{}")) va = propName.ToString().Replace("{}","");
  80. else va = propName.ToString();
  81. propertyBing[propertyInfo.Name] = va;
  82. }
  83. }
  84. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  85. timer.Tick += Timer_Tick; ;
  86. timer.Start();
  87. }
  88. /// <summary>
  89. /// 属性刷新事件
  90. /// </summary>
  91. /// <param name="sender"></param>
  92. /// <param name="e"></param>
  93. private void Timer_Tick(object? sender,EventArgs e)
  94. {
  95. try
  96. {
  97. foreach (var item in propertyBing)
  98. {
  99. //{Binding 测试设备.VAR_A_2}
  100. string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split(".");
  101. if (str.Length > 1)
  102. {
  103. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0]))
  104. {
  105. Dictionary<string,object> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
  106. if (b != null && b.ContainsKey(str[1]))
  107. {
  108. object _value = b[str[1]];
  109. this.GetType().GetProperty(item.Key).SetValue(this,_value);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. }
  118. }
  119. #endregion
  120. }
  121. }