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

144 lines
4.8 KiB

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