using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Toolkit.Mvvm.ComponentModel; using System.Collections.ObjectModel; using BPASmartClient.Helper; using System.IO; using Newtonsoft.Json; using BPASmartClient.Model; using System.Reflection; using Microsoft.Toolkit.Mvvm.Input; using System.Windows; using BPASmartClient.Business; namespace BPASmartClient.ViewModel { public class DeviceMonitorViewModel : ObservableObject { public DeviceMonitorViewModel() { DeviceSwitch = new RelayCommand((o) => { if (o is string nameSpace) { DeviceMonitors.Clear(); Assembly.Load(nameSpace.Substring(0, nameSpace.LastIndexOf("."))).GetTypes().ToList().ForEach((type) => { if (type?.BaseType?.Name == "UserControl") { var fe = type.GetConstructor(Type.EmptyTypes).Invoke(null) as FrameworkElement; DeviceMonitors.Add(new Device() { Name = fe.Name, Namespace = type?.FullName, fe = fe }); } }); } }); DeviceWindowSwitch = new RelayCommand((o) => { if (o != null && o is string nameSpace) { var fe = DeviceMonitors.FirstOrDefault(p => p.Namespace == nameSpace); if (fe != null) MainContent = fe.fe; } }); GetData(); } private void GetData() { //var text = TextHelper.GetInstance.ReadTextInfo("StartShop", "DeviceConfig"); //string path = $"{LocaPath.GetInstance().GetDeviceConfigPath}{text}.json"; //if (File.Exists(path)) //{ // string JsonString = File.ReadAllText(path); // var result = JsonConvert.DeserializeObject>(JsonString); // if (result != null) // { // foreach (var shop in result)//店铺集合 // { // foreach (var device in shop.deviceModels)//设备集合 // { // Devices.Add(new Device() { Name = device.DeviceName, Namespace = device.DeviceNamespace }); // } // } // } //} var result = Plugin.GetInstance().GetPlugin()?.deviceConfigModelJsons; if (result != null) { foreach (var shop in result)//店铺集合 { foreach (var device in shop.deviceModels)//设备集合 { Devices.Add(new Device() { Name = device.DeviceName, Namespace = device.DeviceNamespace }); } } } if (Devices.Count > 0) { Devices.ElementAt(0).IsChecked = true; DeviceMonitors.Clear(); Assembly.Load(Devices.ElementAt(0).Namespace.Substring(0, Devices.ElementAt(0).Namespace.LastIndexOf("."))).GetTypes().ToList().ForEach((type) => { if (type?.BaseType?.Name == "UserControl") { var fe = type.GetConstructor(Type.EmptyTypes).Invoke(null) as FrameworkElement; DeviceMonitors.Add(new Device() { Name = fe.Name, Namespace = type?.FullName, fe = fe }); } }); if (DeviceMonitors.Count > 0) { DeviceMonitors.ElementAt(0).IsChecked = true; if (DeviceMonitors.ElementAt(0).fe != null) MainContent = DeviceMonitors.ElementAt(0).fe; } } } public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } } private FrameworkElement _mMainContent; public RelayCommand DeviceSwitch { get; set; } public RelayCommand DeviceWindowSwitch { get; set; } public ObservableCollection Devices { get; set; } = new ObservableCollection(); public ObservableCollection DeviceMonitors { get; set; } = new ObservableCollection(); } public class Device : ObservableObject { public string Name { get { return _mName; } set { _mName = value; OnPropertyChanged(); } } private string _mName; public string Namespace { get { return _mNamespace; } set { _mNamespace = value; OnPropertyChanged(); } } private string _mNamespace; public bool IsChecked { get { return _mIsChecked; } set { _mIsChecked = value; OnPropertyChanged(); } } private bool _mIsChecked; public FrameworkElement fe { get; set; } } }