using BPASmartClient.Compiler; using BPASmartClient.MessageCommunication; using BPASmartClient.MessageCommunication.MsgControl; using BPASmartClient.MessageName; using BPASmartClient.MessageName.EnumHelp; using BPASmartClient.MessageName.发送消息Model; using BPASmartClient.MessageName.接收消息Model; using BPASmartClient.MessageName.接收消息Model.滚动线; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; 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; namespace BPASmartClient.SCADAControl.CustomerControls { /// /// 滚筒线 /// NewConveyorBelt.xaml 的交互逻辑 /// public partial class NewConveyorBelt : UserControl, IExecutable { #region 临时变量 TextBlock textBlock = null; Path Path_mp = null; Path Path_cb = null; Storyboard storyboard = new Storyboard(); Storyboard storyboard1 = new Storyboard(); #endregion public event EventHandler PropertyChange; //声明一个事件 public NewConveyorBelt() { InitializeComponent(); Width = 1200; Height = 400; this.SizeChanged += ConveyorBelt_SizeChanged; ConveyorBeltWidth = 70; Direction = 0; StrokeBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00BEFA")); StrokeDashArray = new DoubleCollection { 1.5, 1.5 }; StrokeFillBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00BEFA")); StrokeThickness = 2; this.Loaded += NewConveyorBelt_Loaded; ; } private void NewConveyorBelt_Loaded(object sender, RoutedEventArgs e) { EventReceiveNameList.CollectionChanged += EventNameList_CollectionChanged; EventSendNameList.CollectionChanged += EventSendNameList_CollectionChanged; } private void EventSendNameList_CollectionChanged(object? sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (EventSendNameList.Count > 0) { try { EventSendNameListStr = JsonConvert.SerializeObject(EventSendNameList); } catch (Exception ex) { } } } private void EventNameList_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (EventReceiveNameList.Count > 0) { try { EventReceiveNameListStr = JsonConvert.SerializeObject(EventReceiveNameList); } catch (Exception ex) { } } } public string ControlType => "滚动线"; private bool isExecuteState; public bool IsExecuteState { get { return isExecuteState; } set { isExecuteState = value; if (IsExecuteState) { IsEnabled = true; Register(); Style = null; } } } #region 内部函数 public void VisualStateManagerData() { storyboard.RepeatBehavior = RepeatBehavior.Forever; DoubleAnimation dbAscending = new DoubleAnimation { From = 0, To = 100, Duration = new Duration(new TimeSpan(0, 0, 20)), }; storyboard.Children.Add(dbAscending); Storyboard.SetTarget(dbAscending, Path_mp); Storyboard.SetTargetProperty(dbAscending, new PropertyPath("StrokeDashOffset")); storyboard1.RepeatBehavior = RepeatBehavior.Forever; DoubleAnimation dbAscending1 = new DoubleAnimation { From = 0, To = -100, Duration = new Duration(new TimeSpan(0, 0, 20)), }; storyboard1.Children.Add(dbAscending1); Storyboard.SetTarget(dbAscending1, Path_mp); Storyboard.SetTargetProperty(dbAscending1, new PropertyPath("StrokeDashOffset")); Refursh(); } private void ConveyorBelt_SizeChanged(object sender, SizeChangedEventArgs e) { //查找 if (!(Path_cb != null && Path_mp != null)) { foreach (Path tb in FindVisualChildren(this)) { // do something with tb here if (tb.Tag != null) { if (tb.Tag.ToString() == "cb") { Path_cb = tb; } else if (tb.Tag.ToString() == "mp") { Path_mp = tb; } } } VisualStateManagerData(); } if (textBlock == null) { foreach (TextBlock tb in FindVisualChildren(this)) { // do something with tb here if (tb.Tag != null) { if (tb.Tag.ToString() == "标题") { textBlock = tb; } } } } if (this.Height > 0 && this.Width > 0) { try { //传送带外边框绘制 PathGeometry geometry = new PathGeometry(); PathFigure pathFigure = new PathFigure(); pathFigure.StartPoint = new Point(this.Height / 2, 0); pathFigure.Segments.Add(new ArcSegment(new Point(this.Height / 2, this.Height), new Size(this.Height / 2, this.Height / 2), 0, false, SweepDirection.Counterclockwise, true)); pathFigure.Segments.Add(new LineSegment(new Point(this.Width, this.Height), true)); pathFigure.Segments.Add(new LineSegment(new Point(this.Width, this.Height - ConveyorBeltWidth), true)); double innerCircle = (this.Height - (ConveyorBeltWidth * 2)) / 2;//内圆半径 pathFigure.Segments.Add(new LineSegment(new Point(ConveyorBeltWidth + innerCircle, this.Height - ConveyorBeltWidth), true)); pathFigure.Segments.Add(new ArcSegment(new Point(ConveyorBeltWidth + innerCircle, ConveyorBeltWidth), new Size(innerCircle, innerCircle), 0, false, SweepDirection.Clockwise, true)); pathFigure.Segments.Add(new LineSegment(new Point(this.Width, ConveyorBeltWidth), true)); pathFigure.Segments.Add(new LineSegment(new Point(this.Width, 0), true)); pathFigure.Segments.Add(new LineSegment(new Point(this.Height / 2, 0), true)); geometry.Figures.Add(pathFigure); Path_cb.Data = geometry; //传送带内部皮带绘制 PathGeometry geometry1 = new PathGeometry(); PathFigure pathFigure1 = new PathFigure(); pathFigure1.StartPoint = new Point(this.Width, ConveyorBeltWidth / 2); double innerCircle1 = (this.Height - ConveyorBeltWidth) / 2;//内圆半径 pathFigure1.Segments.Add(new LineSegment(new Point(innerCircle1 + ConveyorBeltWidth / 2, ConveyorBeltWidth / 2), true)); pathFigure1.Segments.Add(new ArcSegment(new Point(innerCircle1 + ConveyorBeltWidth / 2, this.Height - ConveyorBeltWidth / 2), new Size(innerCircle1, innerCircle1), 0, false, SweepDirection.Counterclockwise, true)); pathFigure1.Segments.Add(new LineSegment(new Point(this.Width, this.Height - ConveyorBeltWidth / 2), true)); geometry1.Figures.Add(pathFigure1); Path_mp.Data = geometry1; } catch (Exception ex) { } } } 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 属性 private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as NewConveyorBelt)?.Refursh(); } public DoubleCollection StrokeDashArray { get { return (DoubleCollection)GetValue(StrokeDashArrayProperty); } set { SetValue(StrokeDashArrayProperty, value); } } public static readonly DependencyProperty StrokeDashArrayProperty = DependencyProperty.Register("StrokeDashArray", typeof(DoubleCollection), typeof(NewConveyorBelt), new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged))); public double ConveyorBeltWidth { get { return (double)GetValue(ConveyorBeltWidthProperty); } set { SetValue(ConveyorBeltWidthProperty, value); } } public static readonly DependencyProperty ConveyorBeltWidthProperty = DependencyProperty.Register("ConveyorBeltWidth", typeof(double), typeof(NewConveyorBelt), new PropertyMetadata(50.0, new PropertyChangedCallback(OnPropertyChanged))); public Brush StrokeFillBrush { get { return (Brush)GetValue(StrokeFillBrushProperty); } set { SetValue(StrokeFillBrushProperty, value); } } public static readonly DependencyProperty StrokeFillBrushProperty = DependencyProperty.Register("StrokeFillBrush", typeof(Brush), typeof(NewConveyorBelt), new PropertyMetadata(Brushes.LightBlue, new PropertyChangedCallback(OnPropertyChanged))); public Brush StrokeBrush { get { return (Brush)GetValue(StrokeBrushProperty); } set { SetValue(StrokeBrushProperty, value); } } public static readonly DependencyProperty StrokeBrushProperty = DependencyProperty.Register("StrokeBrush", typeof(Brush), typeof(NewConveyorBelt), new PropertyMetadata(Brushes.LightBlue, new PropertyChangedCallback(OnPropertyChanged))); public double StrokeThickness { get { return (double)GetValue(StrokeThicknessProperty); } set { SetValue(StrokeThicknessProperty, value); } } public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register("StrokeThickness", typeof(double), typeof(NewConveyorBelt), new PropertyMetadata(1.0, new PropertyChangedCallback(OnPropertyChanged))); [Category("值设定")] public int Direction { get { return (int)GetValue(DirectionProperty); } set { SetValue(DirectionProperty, value); } } public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register("Direction", typeof(int), typeof(NewConveyorBelt), new PropertyMetadata(0, new PropertyChangedCallback(OnPropertyChanged))); [Category("值设定")] public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(new PropertyChangedCallback(onTargetChanged))); private static void onTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.TargetRefresh(); [Category("主题订阅")] public string ConveyorBeltLeft { get { return (string)GetValue(ConveyorBeltLeftProperty); } set { SetValue(ConveyorBeltLeftProperty, value); } } public static readonly DependencyProperty ConveyorBeltLeftProperty = DependencyProperty.Register("ConveyorBeltLeft", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty)); [Category("主题订阅")] public string ConveyorBeltRight { get { return (string)GetValue(ConveyorBeltRightProperty); } set { SetValue(ConveyorBeltRightProperty, value); } } public static readonly DependencyProperty ConveyorBeltRightProperty = DependencyProperty.Register("ConveyorBeltRight", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty)); [Category("主题订阅")] public string ConveyorBeltStop { get { return (string)GetValue(ConveyorBeltStopProperty); } set { SetValue(ConveyorBeltStopProperty, value); } } public static readonly DependencyProperty ConveyorBeltStopProperty = DependencyProperty.Register("ConveyorBeltStop", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty)); [Category("名称[自动生成]")] public string EventReceiveNameListStr { get { return (string)GetValue(EventReceiveNameListStrProperty); } set { SetValue(EventReceiveNameListStrProperty, value); } } public static readonly DependencyProperty EventReceiveNameListStrProperty = DependencyProperty.Register("EventReceiveNameListStr",typeof(string),typeof(NewConveyorBelt),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onEventReceiveNameListStrChanged))); private static void onEventReceiveNameListStrChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.ReceiveNameRefresh(); [Category("消息接收名称集合")] public ObservableCollection EventReceiveNameList { get { return (ObservableCollection)GetValue(EventReceiveNameListProperty); } set { SetValue(EventReceiveNameListProperty, value); } } public static readonly DependencyProperty EventReceiveNameListProperty = DependencyProperty.Register("EventReceiveNameList", typeof(ObservableCollection), typeof(NewConveyorBelt), new PropertyMetadata(new ObservableCollection(), onEventNameListChanged)); private static void onEventNameListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.RunNameRefresh(); [Category("名称[自动生成]")] private string EventSendNameListStr { get { return (string)GetValue(EventSendNameListStrProperty); } set { SetValue(EventSendNameListStrProperty,value); } } private static readonly DependencyProperty EventSendNameListStrProperty = DependencyProperty.Register("EventSendNameListStr",typeof(string),typeof(NewConveyorBelt),new PropertyMetadata(string.Empty)); [Category("消息发送名称集合")] private ObservableCollection EventSendNameList { get { return (ObservableCollection)GetValue(EventSendNameListProperty); } set { SetValue(EventSendNameListProperty,value); } } private static readonly DependencyProperty EventSendNameListProperty = DependencyProperty.Register("EventSendNameList",typeof(ObservableCollection),typeof(NewConveyorBelt),new PropertyMetadata(new ObservableCollection(),new PropertyChangedCallback(onEventNameListChanged))); #endregion #region 属性变更事件 List MessageNameL = null; public void Register() { if (!string.IsNullOrEmpty(EventReceiveNameListStr)) { try { EventReceiveNameList = JsonConvert.DeserializeObject>(EventReceiveNameListStr); } catch (Exception ex) { } } if (!string.IsNullOrEmpty(EventSendNameListStr)) { try { EventSendNameList = JsonConvert.DeserializeObject>(EventSendNameListStr); } catch (Exception ex) { } } List MessageNameNew = EventReceiveNameList?.Select(o => o.MeaageName.ToString())?.Distinct()?.ToList(); if (MessageNameL == null || !MessageNameNew.SequenceEqual(MessageNameL)) { MessageNameL?.ForEach(o => Class_InnerMessageBus.GetInstance().RemoveMessage(o,"NewConveyorBeltEventHandler")); MessageNameNew?.ForEach(x => Class_InnerMessageBus.GetInstance().ListenMessage(this,x,"NewConveyorBeltEventHandler")); MessageNameL = MessageNameNew; } } public void NewConveyorBeltEventHandler(object sender, InnerMessageEventArgs e) { try { if (IsExecuteState = true) { if (e.obj_MessageObj is RunEnumModel)//接收到运行消息 { RunEnumModel mode = (RunEnumModel)e.obj_MessageObj; var msg = EventReceiveNameList?.ToList().Find(par => par.MeaageName.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID); //必对消息号: if (msg != null) { Direction = mode.Run == RunEnum.Run ? 1 : ((mode.Run == RunEnum.Run_Left) ? 2 : 0); } } else if (e.obj_MessageObj is ConveyorBeltMessageModel)//接收到数据模型 { ConveyorBeltMessageModel mode = (ConveyorBeltMessageModel)e.obj_MessageObj; var msg = EventReceiveNameList?.ToList().Find(par => par.MeaageName.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID); //必对消息号: if (msg != null) { Text = mode.Title; } } } } catch (Exception ex) { } } private void Refursh() { if (Direction == 1) { storyboard.Begin(); storyboard1.Stop(); } else if (Direction == 2) { storyboard.Stop(); storyboard1.Begin(); } else { storyboard.Stop(); storyboard1.Stop(); } } /// /// 目标属性刷新 /// public void TargetRefresh() { if (textBlock != null ) { textBlock.Text = Text; } } public void RunNameRefresh() { } /// /// 接收数据改变 /// public void ReceiveNameRefresh() { if (!string.IsNullOrEmpty(EventReceiveNameListStr)) { try { EventReceiveNameList = JsonConvert.DeserializeObject>(EventReceiveNameListStr); } catch (Exception ex) { } } } #endregion } }