|
- 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.Controls.Primitives;
- 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
- {
- /// <summary>
- /// TheToggleButton.xaml 的交互逻辑
- /// </summary>
- public partial class TheToggleButton : ToggleButton, IExecutable
- {
- public event EventHandler PropertyChange; //声明一个事件
-
- /// <summary>
- /// 开关按钮
- /// </summary>
- public TheToggleButton()
- {
- InitializeComponent();
- Width = 80;
- Height = 30;
- ResourceDictionary languageResDic = new ResourceDictionary();
- languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute);
- this.Resources.MergedDictionaries.Add(languageResDic);
- }
-
- public string ControlType => "控件";
-
- private bool isExecuteState;
- public bool IsExecuteState
- {
- get { return isExecuteState; }
- set
- {
- isExecuteState = value;
- if (IsExecuteState)
- {
- //Style = Application.Current.Resources["ExecuteToggleButton"] as Style;//FindResource("ExecuteToggleButton") as Style;
-
- Register();
- }
- }
- }
- [Category("事件")]
- public string BindingIsChecked
- {
- get { return (string)GetValue(BindingIsCheckedProperty); }
- set { SetValue(BindingIsCheckedProperty, value); }
- }
- public static readonly DependencyProperty BindingIsCheckedProperty =
- DependencyProperty.Register("BindingIsChecked", typeof(string),typeof(TheToggleButton),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(TheToggleButton),new PropertyMetadata(string.Empty));
- /// <summary>
- /// 不勾选时执行代码
- /// </summary>
- [Category("事件")]
- public string UnCheckedExec
- {
- get { return (string)GetValue(UnCheckedExecProperty); }
- set { SetValue(UnCheckedExecProperty, value); }
- }
- public static readonly DependencyProperty UnCheckedExecProperty =
- DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(TheToggleButton), new PropertyMetadata(string.Empty));
-
- /// <summary>
- /// 勾选时执行代码
- /// </summary>
- [Category("事件")]
- public string CheckedExec
- {
- get { return (string)GetValue(CheckedExecProperty); }
- set { SetValue(CheckedExecProperty, value); }
- }
- public static readonly DependencyProperty CheckedExecProperty =
- DependencyProperty.Register("CheckedExec", typeof(string), typeof(TheToggleButton), 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(TheToggleButton),new PropertyMetadata(100));
- /// <summary>
- /// 属性刷新器
- /// </summary>
- DispatcherTimer timer = new DispatcherTimer();
- /// <summary>
- /// 属性绑定变量集合
- /// </summary>
- Dictionary<string,string> propertyBing = new Dictionary<string,string>();
- 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();
- }
- Click += TheToggleButton_Click;
- Checked += TheCheckBox_Checked;
- Unchecked += TheCheckBox_Unchecked;
- }
-
-
-
- /// <summary>
- /// 属性刷新事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<string,DeviceDataModel> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
- if (b != null && b.ContainsKey(str[1]))
- {
- object _value = b[str[1]].VarVaule;
- bool _checked = false;
- try
- {
- _checked = bool.Parse(_value.ToString());
- }
- catch (Exception ex)
- {
- _checked = false;
- }
- EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType);
- SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = _checked.ToString(),DataType = eDataType });
- this.GetType().GetProperty("IsChecked").SetValue(this,_checked);
-
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
-
- private void TheToggleButton_Click(object sender,RoutedEventArgs e)
- {
- 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<string,DeviceDataModel> 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 = "False",DataType = eDataType });
- }
- }
- }
- }
- if (this.IsChecked == true) Config.GetInstance().RunJsScipt(CheckedExec);
- else Config.GetInstance().RunJsScipt(UnCheckedExec);
- timer.Start();
- }
-
-
- private void TheCheckBox_Unchecked(object sender, RoutedEventArgs e)
- {
-
- }
-
- private void TheCheckBox_Checked(object sender, RoutedEventArgs e)
- {
-
- }
- }
- }
|