|
- using BPASmart.Model;
- using BPASmartClient.Compiler;
- using BPASmartClient.DATABUS;
- 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.Animation;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
-
- namespace BPASmartClient.SCADAControl.CustomerControls
- {
- /// <summary>
- /// 开关button
- /// </summary>
- [TemplatePart(Name = ELLIPSE, Type = typeof(FrameworkElement))]
- [TemplatePart(Name = TranslateX, Type = typeof(TranslateTransform))]
- public class SwitchButton : ToggleButton, IExecutable
- {
- public event EventHandler PropertyChange; //声明一个事件
-
- public SwitchButton()
- {
- SetCurrentValue(WidthProperty, 120d);
- SetCurrentValue(HeightProperty, 40d);
- }
-
- public const string ELLIPSE = "ELLIPSE";
- public const string TranslateX = "TranslateX";
- static SwitchButton()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(SwitchButton), new FrameworkPropertyMetadata(typeof(SwitchButton)));
- }
- [Category("事件")]
- public string BindingIsChecked
- {
- get { return (string)GetValue(BingIsCheckedProperty); }
- set { SetValue(BingIsCheckedProperty, value); }
- }
- public static readonly DependencyProperty BingIsCheckedProperty =
- DependencyProperty.Register("BindingIsChecked", typeof(string),typeof(SwitchButton),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(SwitchButton),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(SwitchButton), 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(SwitchButton), 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(SwitchButton),new PropertyMetadata(100));
- protected override void OnRender(DrawingContext drawingContext)
- {
- base.OnRender(drawingContext);
- ellipse.Width = Height - 8;
- ellipse.Height = Height - 8;
- Refresh();
- }
-
- TranslateTransform transX;
- Ellipse ellipse;
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- ellipse = GetTemplateChild(ELLIPSE) as Ellipse;
- transX = GetTemplateChild(TranslateX) as TranslateTransform;
- }
- protected override void OnChecked(RoutedEventArgs e)
- {
- base.OnChecked(e);
- Refresh();
- }
- protected override void OnUnchecked(RoutedEventArgs e)
- {
- base.OnUnchecked(e);
- Refresh();
- }
-
- void Refresh()
- {
- if (ellipse == null)
- {
- return;
- }
- DoubleAnimation da = new DoubleAnimation();
- da.Duration = new Duration(TimeSpan.FromMilliseconds(250));
- da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
- if (IsChecked == true)
- {
- da.To = ActualWidth - ellipse.ActualWidth - 5;
- ellipse.SetCurrentValue(Ellipse.FillProperty, Background);
- }
- else
- {
- da.To = 3;
- ellipse.SetCurrentValue(Ellipse.FillProperty, Brushes.Gray);
- }
-
- transX.BeginAnimation(TranslateTransform.XProperty, da);
- }
-
- private bool isExecuteState;
- public bool IsExecuteState
- {
- get { return isExecuteState; }
- set
- {
- isExecuteState = value;
- if (IsExecuteState)
- {
- Register();
- }
- }
- }
- /// <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();
- }
- this.Click += SwitchButton_Click;
- }
-
-
-
- /// <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 SwitchButton_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 = this.IsChecked.ToString(),DataType = eDataType });
- }
- }
- }
- }
- if(this.IsChecked==true) Config.GetInstance().RunJsScipt(CheckedExec);
- else Config.GetInstance().RunJsScipt(UnCheckedExec);
- timer.Start();
- }
-
- public string ControlType => "控件";
-
- }
-
-
- }
|