using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.Helper; using BPASmartClient.Model; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; namespace BPASmartClient.CustomResource.Pages.ViewModel { public class MainViewModel : ObservableObject { public MainViewModel() { NavChangedCommand = new RelayCommand(DoNavChanged); BPA.Helper.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); }); }); 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"); } 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).ToggleWindowPath); DoNavChanged(menuModels.ElementAt(MainIndex).subMenumodels.ElementAt(SubIndex)); } } } 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); } } } private bool _status; /// /// 设备初始化状态 /// public bool Status { get { return _status; } set { if (value) { ActionManage.GetInstance.Send("StartPlcInite"); } else { ActionManage.GetInstance.Send("EndPlcInite"); } _status = value; OnPropertyChanged(); } } /// /// 开机自启 /// public bool AutoStart { get { return SystemHelper.GetInstance.IsAutoStart(); } set { SystemHelper.GetInstance.AutoStart(value); OnPropertyChanged(); } } public RelayCommand 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; } }