using DefaultEdit.Communication; using DefaultEdit.Core; using DefaultEdit.Log4xml; using DefaultEdit.Model; using DefaultEdit.Views; using Microsoft.Xaml.Behaviors; 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.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 _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) ); 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() { } void ClearRunData() { Management.RuningData.Clear(); } void ClearLogData() { Logs.Clear(); } void RoleLoginViewShow() { RoleLoginView roleLoginView = new RoleLoginView(); roleLoginView.ShowDialog(); } } 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(); } } }