using BPASmart.Model; using BPASmartClient.Compiler; using BPASmartClient.DATABUS; using BPASmartClient.SCADAControl; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace BPASmartClient.SCADAControl.CustomerControls { public class TheTextBox : TextBox, IExecutable { static TheTextBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TheTextBox), new FrameworkPropertyMetadata(typeof(TheTextBox))); } public TheTextBox() { ResourceDictionary languageResDic = new ResourceDictionary(); languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); this.Resources.MergedDictionaries.Add(languageResDic); Height = 30; Width = 80; FontSize = 16; this.KeyDown += TheTextBox_KeyDown; this.TextChanged += TheTextBox_TextChanged; } bool isRun=false; private void TheTextBox_KeyDown(object sender,KeyEventArgs e) { isRun = true; } private void TheTextBox_TextChanged(object sender,TextChangedEventArgs e) { if (isRun) { timer.Stop(); foreach (var item in propertyBing) { //{Binding 测试设备.VAR_A_2} string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) { Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; if (b != null && b.ContainsKey(str[1])) { object _value = b[str[1]].VarVaule; EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType); SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = Text,DataType = eDataType }); } } } } Config.GetInstance().RunJsScipt(ValueChangedExecute); timer.Start(); isRun=false; } } public string ControlType => "控件"; private bool isExecuteState; public bool IsExecuteState { get { return isExecuteState; } set { isExecuteState = value; if (IsExecuteState) { IsEnabled = true; Focusable = true; Register(); //Style = null; } } } #region 数据绑定模块 public string ValueChangedExecute { get { return (string)GetValue(ValueChangedExecuteProperty); } set { SetValue(ValueChangedExecuteProperty,value); } } public static readonly DependencyProperty ValueChangedExecuteProperty = DependencyProperty.Register("ValueChangedExecute",typeof(string),typeof(TheTextBox),new PropertyMetadata(string.Empty)); [Category("事件")] public string SendText { get { return (string)GetValue(SendTextProperty); } set { SetValue(SendTextProperty, value); } } public static readonly DependencyProperty SendTextProperty = DependencyProperty.Register("SendText", typeof(string),typeof(TheTextBox),new PropertyMetadata(string.Empty)); [Category("数据绑定-数据来源")] public int TimeCount { get { return (int)GetValue(TimeCountProperty); } set { SetValue(TimeCountProperty,value); } } public static readonly DependencyProperty TimeCountProperty = DependencyProperty.Register("TimeCount",typeof(int),typeof(TheTextBox),new PropertyMetadata(100)); public event EventHandler PropertyChange; //声明一个事件 /// /// 属性刷新器 /// DispatcherTimer timer = new DispatcherTimer(); /// /// 属性绑定变量集合 /// Dictionary propertyBing = new Dictionary(); /// /// 运行事件 /// public void Register() { PropertyInfo[] propertyInfos = this.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { var propName = propertyInfo?.GetValue(this,null); if (propName is string && propName != null && propName.ToString().Contains("Binding ") && propName.ToString().Contains(".")) { propertyBing[propertyInfo.Name] = propName.ToString(); } } if (propertyBing.Count > 0) { timer.Interval = TimeSpan.FromMilliseconds(TimeCount); timer.Tick += Timer_Tick; ; timer.Start(); } } /// /// 属性刷新事件 /// /// /// private void Timer_Tick(object? sender,EventArgs e) { try { foreach (var item in propertyBing) { //{Binding 测试设备.VAR_A_2} string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) { Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; if (b != null && b.ContainsKey(str[1])) { object _value = b[str[1]].VarVaule; this.GetType().GetProperty(item.Key).SetValue(this,_value); } } } } } catch (Exception ex) { } } #endregion } }