using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.Helper; using System.Collections.ObjectModel; using BPA.Helper; using System.IO; using Newtonsoft.Json; using BPASmartClient.Model; using System.Reflection; using System.Windows; using BPASmartClient.Business; namespace BPASmartClient.ViewModel { public class DeviceMonitorViewModel : NotifyBase { public DeviceMonitorViewModel() { DeviceWindowSwitch = new BPARelayCommand((o) => { if (o != null && o is string nameSpace) { foreach (var item in Devices) { var fe = item.DeviceMonitors.FirstOrDefault(p => p.Namespace == nameSpace); if (fe != null) { MainContent = fe.fe; break; } } } }); GetData(); } private void GetData() { var result = Plugin.GetInstance.GetPlugin()?.deviceConfigModelJsons; if (result != null) { foreach (var shop in result)//店铺集合 { foreach (var device in shop.deviceModels)//设备集合 { Devices.Add(new ControlDevice() { Name = device.DeviceName, Namespace = device.DeviceNamespace }); } } } if (Devices.Count > 0) { Devices.ElementAt(0).IsChecked = true; for (int i = 0; i < Devices.Count; i++) { Devices.ElementAt(i).DeviceMonitors.Clear(); Assembly.Load(Devices.ElementAt(i).Namespace.Substring(0, Devices.ElementAt(i).Namespace.LastIndexOf("."))).GetTypes().ToList().ForEach((type) => { if (type?.BaseType?.Name == "UserControl") { var fe = type.GetConstructor(Type.EmptyTypes).Invoke(null) as FrameworkElement; Devices.ElementAt(i).DeviceMonitors.Add(new Device() { Name = fe.Name, Namespace = type?.FullName, fe = fe }); } }); } if (Devices.ElementAt(0).DeviceMonitors.Count > 0) { Devices.ElementAt(0).DeviceMonitors.ElementAt(0).IsChecked = true; if (Devices.ElementAt(0).DeviceMonitors.ElementAt(0).fe != null) MainContent = Devices.ElementAt(0).DeviceMonitors.ElementAt(0).fe; } } } public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } } private FrameworkElement _mMainContent; public BPARelayCommand DeviceSwitch { get; set; } public BPARelayCommand DeviceWindowSwitch { get; set; } public ObservableCollection Devices { get; set; } = new ObservableCollection(); } public class ControlDevice : NotifyBase { 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 ObservableCollection DeviceMonitors { get; set; } = new ObservableCollection(); } public class Device : NotifyBase { 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; } } }