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 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); menuModels = MenuManage.GetInstance.menuModels; PermissionChange(); ActionManage.GetInstance.Register(new Action(() => { PermissionChange(); }), "PermissionChange"); //测试用 Global.DeviceRawMaterials.Add(new RawMaterialModel { RawMaterialName = "营养强化剂", DeviceIp = "192.168.1.111", RawMaterialSource = 1 }); Global.DeviceRawMaterials.Add(new RawMaterialModel { RawMaterialName = "甜味剂", DeviceIp = "192.168.1.112", RawMaterialSource = 1 }); Global.DeviceRawMaterials.Add(new RawMaterialModel { RawMaterialName = "抗氧化剂", DeviceIp = "192.168.1.113", RawMaterialSource = 1 }); Global.DeviceRawMaterials.Add(new RawMaterialModel { RawMaterialName = "食用香料", DeviceIp = "192.168.1.114", RawMaterialSource = 1 }); } 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); } } } public ObservableCollection menuModels { get; set; } private void DoNavChanged(object obj) { ActionManage.GetInstance.Send("RecipeIsChange"); for (int i = 0; i < menuModels.Count; i++) { var res = menuModels.ElementAt(i).subMenumodels.FirstOrDefault(p => p.ToggleWindowPath == obj.ToString()); if (res != null) { DisplayName = res.SubMenuName; var end = res.AssemblyName.Substring(res.AssemblyName.Length - 1); var start = res.ToggleWindowPath.Substring(0, 1); bool isAddPoint = end != "." && start != "."; string point = isAddPoint ? "." : ""; Type type = Assembly.Load(res.AssemblyName)?.GetType($"{res.AssemblyName }{point}{ res.ToggleWindowPath}"); ConstructorInfo cti = type?.GetConstructor(System.Type.EmptyTypes); if (type?.BaseType.Name == "Window") { ((Window)cti?.Invoke(null)).ShowDialog(); break; } else if (type?.BaseType.Name == "UserControl") { //ConstructorInfo cti = type?.GetConstructor(System.Type.EmptyTypes); MainContent = (FrameworkElement)cti?.Invoke(null); break; } } } } 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; } }