using DefaultEdit.Communication; using DefaultEdit.Core; using DefaultEdit.Log4xml; using DefaultEdit.Model; using DefaultEdit.Views; using Microsoft.Xaml.Behaviors; using NextTreatMesDemo.Models; using Prism.Commands; using Prism.Events; using Prism.Ioc; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Diagnostics; using System.Threading; using System.Windows; using System.Windows.Controls; namespace DefaultEdit.ViewModels { public class WorkRunViewModel : BindableBase { private Management _management; private IContainerProvider _container; private IEventAggregator _eventAggregator; private IRegionManager _regionManager; private ObservableCollection _Logs = new ObservableCollection(); public ObservableCollection Logs { get => _Logs; set => _Logs = value; } public Management Management { get => _management; set => _management = value; } private DelegateCommand _OpenRoleLoginViewCommand; public DelegateCommand OpenRoleLoginViewCommand => _OpenRoleLoginViewCommand ?? ( _OpenRoleLoginViewCommand = new DelegateCommand(RoleLoginViewShow) ); private DelegateCommand _DLoginCommand; public DelegateCommand DLoginCommand => _DLoginCommand ?? ( _DLoginCommand = new DelegateCommand(DLoginViewShow) ); private DelegateCommand _SwitchModeCommand; public DelegateCommand SwitchModeCommand => _SwitchModeCommand ?? ( _SwitchModeCommand = new DelegateCommand(SwitchModeMethod) ); private DelegateCommand _ClearRunDataCommand; public DelegateCommand ClearRunDataCommand => _ClearRunDataCommand ?? ( _ClearRunDataCommand = new DelegateCommand(ClearRunData) ); private DelegateCommand _ClearLogDataCommand; public DelegateCommand ClearLogDataCommand => _ClearLogDataCommand ?? ( _ClearLogDataCommand = new DelegateCommand(ClearLogData) ); private DelegateCommand _ManualPrintCommand; public DelegateCommand ManualPrintCommand => _ManualPrintCommand ?? ( _ManualPrintCommand = new DelegateCommand(ManualPrint) ); public WorkRunViewModel(IContainerProvider container, IRegionManager regionManager, IEventAggregator eventAggregator) { _container = container; _eventAggregator = eventAggregator; _regionManager = regionManager; Management = container.Resolve(); LogHelper.logChangHandler += ShowLog; LogHelper.Info("===========================程序启动=========================="); Management.CurentApplicationSettings = FileHelper.ReadApplicationConfiguration(); LogHelper.Info("加载配置文件完成"); Management.ConnectPlc(); //创建一个定时器,每秒更新一次时间 var timer = new System.Timers.Timer(1000); timer.Elapsed += (sender, e) => { Management.DateTimeNow = DateTime.Now; }; timer.Start(); } private void ShowLog(Log4xml.Log log) { System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { string str = log.Msg.Trim(); while ( true ) { if ( str.Length > 80 ) { Logs.Add(str.Substring(0, 80)); str = log.Time + " " + str.Substring(80); } else { Logs.Add(str); break; } } if ( Logs.Count > 100 ) { Logs.RemoveAt(0); } })); } void SwitchModeMethod() { if ( Management.Ismeswork ) { Management.Ismeswork = false; } else { Management.Ismeswork = true; } } void ClearRunData() { Management.RuningData.Clear(); } void ClearLogData() { Logs.Clear(); } void RoleLoginViewShow() { RoleLoginView roleLoginView = new RoleLoginView(); roleLoginView.ShowDialog(); } void DLoginViewShow() { Management.UserId = ""; Management.UserName = ""; } void ManualPrint(string rfid) { bool isok=false; for ( int i = 0; i < Management.processDataModels.Count; i++ ) { if ( Management.processDataModels[ i ].BoardId == rfid ) { Management.PrintInfo(Management.processDataModels[ i ], "手动"); Management.processDataModels.RemoveAt(i); isok=true; MessageBox.Show("打印完成"); } } if ( !isok ) { MessageBox.Show("网板信息未找到"); } } } public class AutoScrollBehavior : Behavior { protected override void OnAttached() { base.OnAttached(); var items = AssociatedObject.Items; if ( items is INotifyCollectionChanged incc ) { incc.CollectionChanged += OnCollectionChanged; } } private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if ( e.Action == NotifyCollectionChangedAction.Add && AssociatedObject.HasItems ) { AssociatedObject.Dispatcher.BeginInvoke(new Action(() => { AssociatedObject.ScrollIntoView(AssociatedObject.Items[ AssociatedObject.Items.Count - 1 ]); })); } } protected override void OnDetaching() { if ( AssociatedObject.Items is INotifyCollectionChanged incc ) { incc.CollectionChanged -= OnCollectionChanged; } base.OnDetaching(); } } }