using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using System.Security.Cryptography.Xml; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using BPASmartClient.CustomResource.Pages.Model; using BPA.Helper; using BPASmartClient.Model; using BPA.Helper; namespace BPASmartClient.CustomResource.Pages.ViewModel { public class MainViewModel : NotifyBase { public MainViewModel() { NavChangedCommand = new BPARelayCommand(DoNavChanged); MessageLog.GetInstance.NotifyShow = new Action((o) => { Application.Current?.Dispatcher?.Invoke(() => { //ff20aefe var temp = new MessageModel() { LogInfo = o, Forground = new SolidColorBrush(Color.FromArgb(255, 32, 174, 254)) }; if (DebugLogViewModel.MessageModels.Count > 0) DebugLogViewModel.MessageModels.Insert(0, temp); else DebugLogViewModel.MessageModels.Add(temp); }); }); MessageLog.GetInstance.NotifyShowEx = new Action((o) => { Application.Current?.Dispatcher?.Invoke(() => { //FFF53F62 var temp = new MessageModel() { LogInfo = o, Forground = new SolidColorBrush(Color.FromArgb(255, 245, 63, 98)) }; if (DebugLogViewModel.MessageModels.Count > 0) DebugLogViewModel.MessageModels.Insert(0, temp); else DebugLogViewModel.MessageModels.Add(temp); }); }); BPA.Helper.MessageLog.GetInstance.NotifyShow = new Action((o) => { try { Application.Current?.Dispatcher?.Invoke(() => { //ff20aefe var temp = new MessageModel() { LogInfo = o, Forground = new SolidColorBrush(Color.FromArgb(255, 32, 174, 254)) }; if (DebugLogViewModel.MessageModels.Count > 0) DebugLogViewModel.MessageModels.Insert(0, temp); else DebugLogViewModel.MessageModels.Add(temp); }); } catch (Exception) { //throw; } }); BPA.Helper.MessageLog.GetInstance.NotifyShowEx = new Action((o) => { Application.Current?.Dispatcher?.Invoke(() => { //FFF53F62 var temp = new MessageModel() { LogInfo = o, Forground = new SolidColorBrush(Color.FromArgb(255, 245, 63, 98)) }; if (DebugLogViewModel.MessageModels.Count > 0) DebugLogViewModel.MessageModels.Insert(0, temp); else DebugLogViewModel.MessageModels.Add(temp); }); }); menuModels = MenuManage.GetInstance.menuModels; PermissionChange(); ActionManage.GetInstance.Register(new Action(() => { PermissionChange(); }), "PermissionChange"); ActionManage.GetInstance.Register(new Action((c) => { if (c != null) IsAlarm = Convert.ToBoolean(c); }), "HeartBeatCheck"); LogInit(); } private void PermissionChange() { int MainIndex = Array.FindIndex(menuModels.ToArray(), P => P.MainMenuVisibility == Visibility.Visible); if (MainIndex >= 0 && MainIndex < menuModels.Count) { int SubIndex; SubIndex = Array.FindIndex(menuModels.ElementAt(MainIndex).subMenumodels.ToArray(), p => p.SubMenuVisibility == Visibility.Visible); if (SubIndex >= 0 && SubIndex < menuModels.ElementAt(MainIndex).subMenumodels.Count) { DoNavChanged(menuModels.ElementAt(MainIndex).subMenumodels.ElementAt(SubIndex)); } } } private void LogInit() { LogHelper.GetInstance.Init(); LogHelper.GetInstance.ShowAlarmLog = s => { //if (AlarmViewModel.AlarmInfos == null) AlarmViewModel.AlarmInfos = new ObservableCollection(); Alarm alarm = new Alarm() { Date = s.Date, Grade = s.Grade, NumId = AlarmViewModel.AlarmInfos.Count, Info = s.MsgInfo, Time = s.Time, Value = s.Value }; Application.Current?.Dispatcher.Invoke(() => { if (AlarmViewModel.AlarmInfos.Count <= 0) AlarmViewModel.AlarmInfos.Add(alarm); else if (AlarmViewModel.AlarmInfos.Count > 0) AlarmViewModel.AlarmInfos.Insert(0, alarm); }); for (int i = 0; i < AlarmViewModel.AlarmInfos.Count; i++) { AlarmViewModel.AlarmInfos[i].NumId = i + 1; } }; } public ObservableCollection menuModels { get; set; } private void DoNavChanged(object obj) { if (obj != null && obj is SubMenumodel menumodel) { DisplayName = menumodel.SubMenuName; var end = menumodel.AssemblyName.Substring(menumodel.AssemblyName.Length - 1); var start = menumodel.ToggleWindowPath.Substring(0, 1); bool isAddPoint = end != "." && start != "."; string point = isAddPoint ? "." : ""; Type type = Assembly.Load(menumodel.AssemblyName)?.GetType($"{menumodel.AssemblyName}{point}{menumodel.ToggleWindowPath}"); ConstructorInfo cti = type?.GetConstructor(System.Type.EmptyTypes); if (type?.BaseType.Name == "Window") ((Window)cti?.Invoke(null)).ShowDialog(); else if (type?.BaseType.Name == "UserControl") { MainContent = null; MainContent = (FrameworkElement)cti?.Invoke(null); MainContent?.GetType()?.GetProperty("Name").SetValue(MainContent, menumodel.SubMenuName); if (MainContent is null) { var instance = type?.GetProperty("GetInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance); MainContent = null; MainContent = (FrameworkElement)instance?.GetValue(null); MainContent?.GetType()?.GetProperty("Name").SetValue(MainContent, menumodel.SubMenuName); } } } } private bool _status; /// /// 设备初始化状态 /// public bool Status { get { return _status; } set { if (value) { ActionManage.GetInstance.Send("StartPlcInite"); } else { ActionManage.GetInstance.Send("EndPlcInite"); } _status = value; OnPropertyChanged(); } } private bool _isAlarm = true; /// /// 心跳检测报警 /// public bool IsAlarm { get { return _isAlarm; } set { _isAlarm = value; OnPropertyChanged(); } } /// /// 开机自启 /// public bool AutoStart { get { return SystemHelper.GetInstance.IsAutoStart(); } set { SystemHelper.GetInstance.AutoStart(value); OnPropertyChanged(); } } public BPARelayCommand NavChangedCommand { get; set; } public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } } private FrameworkElement _mMainContent; public string DisplayName { get { return _mDisplayName; } set { _mDisplayName = value; OnPropertyChanged(); } } private string _mDisplayName; } }