using BPASmartClient.Compiler;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing.Design;
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace BPASmartClient.SCADAControl.CustomerControls
{
///
/// Silos.xaml 的交互逻辑
/// 物料仓
///
public partial class Silos :UserControl, IExecutable, IDisposable
{
#region 临时变量
TextBlock textBlockCLKZ = null;
TextBlock textBlockValue = null;
TextBlock textBlockText = null;
TextBlock textBlockTitle = null;
Ellipse ellipseControl = null;
Storyboard storyboard = new Storyboard();
#endregion
public Silos()
{
InitializeComponent();
Width = 180;
Height = 270;
this.DataContext = this;
this.SizeChanged += Silos_SizeChanged; ;
}
public string ControlType => "物料仓";
private bool isExecuteState;
public bool IsExecuteState
{
get { return isExecuteState; }
set
{
isExecuteState = value;
if (IsExecuteState)
{
IsEnabled = true;
Register();
//Style = null;
}
}
}
#region 动画
///
/// 加载内部变量
///
private void Silos_SizeChanged(object sender,SizeChangedEventArgs e)
{
//查找
if (ellipseControl == null)
{
foreach (Ellipse tb in FindVisualChildren(this))
{
// do something with tb here
if (tb.Tag != null)
{
if (tb.Tag.ToString() == "出料圆")
{
ellipseControl = tb;
storyboard.RepeatBehavior = RepeatBehavior.Forever;
DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTarget(animation,ellipseControl);
Storyboard.SetTargetProperty(animation,new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"));
animation.KeyFrames.Add(new EasingDoubleKeyFrame(0,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))));
animation.KeyFrames.Add(new EasingDoubleKeyFrame(180,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));
animation.KeyFrames.Add(new EasingDoubleKeyFrame(360,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2))));
storyboard.Children.Add(animation);
}
}
}
foreach (TextBlock tb in FindVisualChildren(this))
{
// do something with tb here
if (tb.Tag != null)
{
if (tb.Tag.ToString() == "出料控制")
{
textBlockCLKZ = tb;
}
else if (tb.Tag.ToString() == "Value")
{
textBlockValue = tb;
}
else if (tb.Tag.ToString() == "Text")
{
textBlockText = tb;
}
else if (tb.Tag.ToString() == "Title")
{
textBlockTitle = tb;
}
}
}
foreach (StackPanel tb in FindVisualChildren(this))
{
// do something with tb here
if (tb.Tag != null)
{
if (tb.Tag.ToString() == "ControlEvent")
{
foreach (var item in tb.Children)
{
if (item is Image)
{
(item as Image).MouseLeftButtonDown += Image_MouseLeftButtonDown; ;
}
}
}
}
}
TargetRefresh();
}
}
public static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj,i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren(child))
{
yield return childOfChild;
}
}
}
}
#endregion
#region 属性
[Category("值设定")]
public string KgValue
{
get { return (string)GetValue(KgValueProperty); }
set { SetValue(KgValueProperty, value); }
}
public static readonly DependencyProperty KgValueProperty =
DependencyProperty.Register("KgValue", typeof(string),typeof(Silos),new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
private static void onTargetChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as Silos)?.TargetRefresh();
[Category("值设定")]
public string WLText
{
get { return (string)GetValue(WLTextProperty); }
set { SetValue(WLTextProperty, value); }
}
public static readonly DependencyProperty WLTextProperty =
DependencyProperty.Register("WLText", typeof(string),typeof(Silos),new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
[Category("值设定")]
public string WLTitle
{
get { return (string)GetValue(WLTitleProperty); }
set { SetValue(WLTitleProperty, value); }
}
public static readonly DependencyProperty WLTitleProperty =
DependencyProperty.Register("WLTitle", typeof(string),typeof(Silos),new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
[Category("值设定")]
public int Direction
{
get { return (int)GetValue(DirectionProperty); }
set { SetValue(DirectionProperty,value); }
}
public static readonly DependencyProperty DirectionProperty =
DependencyProperty.Register("Direction",typeof(int),typeof(Silos),
new PropertyMetadata(0,new PropertyChangedCallback(OnPropertyChanged)));
private static void OnPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
(d as Silos)?.Refresh();
}
#endregion
#region 数据绑定模块
public string SendText
{
get { return (string)GetValue(SendTextProperty); }
set { SetValue(SendTextProperty, value); }
}
public static readonly DependencyProperty SendTextProperty =
DependencyProperty.Register("SendText", typeof(string), typeof(Silos), new PropertyMetadata(string.Empty));
public string ChuLiaoExecute
{
get { return (string)GetValue(ChuLiaoExecuteProperty); }
set { SetValue(ChuLiaoExecuteProperty, value); }
}
public static readonly DependencyProperty ChuLiaoExecuteProperty =
DependencyProperty.Register("ChuLiaoExecute", typeof(string), typeof(Silos), new PropertyMetadata(string.Empty));
public string StopChuLiaoExecute
{
get { return (string)GetValue(StopChuLiaoExecuteProperty); }
set { SetValue(StopChuLiaoExecuteProperty, value); }
}
public static readonly DependencyProperty StopChuLiaoExecuteProperty =
DependencyProperty.Register("StopChuLiaoExecute", typeof(string), typeof(Silos), new PropertyMetadata(string.Empty));
public string ValueChangedExecute
{
get { return (string)GetValue(ValueChangedExecuteProperty); }
set { SetValue(ValueChangedExecuteProperty, value); }
}
public static readonly DependencyProperty ValueChangedExecuteProperty =
DependencyProperty.Register("ValueChangedExecute", typeof(string), typeof(Silos), new PropertyMetadata(string.Empty));
public event EventHandler PropertyChange; //声明一个事件
///
/// 数据模板
///
private Dictionary DataModel
{
get { return (Dictionary)GetValue(DataModelProperty); }
set { SetValue(DataModelProperty, value); }
}
private static readonly DependencyProperty DataModelProperty =
DependencyProperty.Register("DataModel", typeof(Dictionary), typeof(Silos), new PropertyMetadata(new Dictionary()));
#endregion
#region 函数
///
/// 目标属性刷新
///
public void TargetRefresh()
{
if (textBlockValue != null && textBlockText != null && textBlockTitle != null)
{
textBlockValue.Text = KgValue;
textBlockText.Text = WLText;
textBlockTitle.Text = WLTitle;
}
}
///
/// 值变化刷新
///
public void Refresh()
{
if (ellipseControl != null)
{
if (Direction == 1)
{
ellipseControl.Visibility = Visibility.Visible;
textBlockCLKZ.Visibility = Visibility.Visible;
storyboard.Begin();
}
else if (Direction == 2)
{
ellipseControl.Visibility = Visibility.Collapsed;
textBlockCLKZ.Visibility = Visibility.Collapsed;
storyboard.Stop();
}
else
{
ellipseControl.Visibility = Visibility.Collapsed;
textBlockCLKZ.Visibility = Visibility.Collapsed;
storyboard.Stop();
}
}
}
#endregion
#region 运行事件
List MessageNameL = null;
public void Register()
{
Class_DataBus.GetInstance().BindingAction += BindingActionHeader;
}
public void BindingActionHeader(object sender, EventArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding;
PropertyChange?.Invoke(this, EventArgs.Empty);
}));
}
#endregion
#region 发送消息事件
public void Dispose()
{
}
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
Image image = sender as Image;
if (image.Tag != null)
{
if (image.Tag.ToString() == "出料")
{
Config.GetInstance().RunJsScipt(ChuLiaoExecute);
}
else if (image.Tag.ToString() == "停止出料")
{
Config.GetInstance().RunJsScipt(StopChuLiaoExecute);
}
}
}
catch (Exception ex)
{
}
}
#endregion
}
}