123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.UI.MVVM;
- namespace MECF.Framework.RT.Core.Backend
- {
- public class NotifiableSCConfigItem : SCConfigItem, INotifyPropertyChanged
- {
- public string SetPoint { get; set; }
- public event PropertyChangedEventHandler PropertyChanged;
- public void InvokePropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public void InvokePropertyChanged()
- {
- PropertyInfo[] ps = this.GetType().GetProperties();
- foreach (PropertyInfo p in ps)
- {
- InvokePropertyChanged(p.Name);
- if (p.PropertyType == typeof(ICommand))
- {
- DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;
- if (cmd != null)
- cmd.RaiseCanExecuteChanged();
- }
- }
- FieldInfo[] fi = this.GetType().GetFields();
- foreach (FieldInfo p in fi)
- {
- InvokePropertyChanged(p.Name);
- if (p.FieldType == typeof(ICommand))
- {
- DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
- if (cmd != null)
- cmd.RaiseCanExecuteChanged();
- }
- }
- }
- }
- public class BackendSCConfigViewModel : ViewModelBase
- {
- private List<SCConfigItem> _scItems;
- public ObservableCollection<NotifiableSCConfigItem> ScItemList { get; set; }
- public ICommand SetScCommand { get; set; }
- public ICommand ReloadCommand { get; set; }
- public BackendSCConfigViewModel()
- {
- ScItemList = new ObservableCollection<NotifiableSCConfigItem>();
- Init();
- SetScCommand = new DelegateCommand<string>(SetSc);
- ReloadCommand = new DelegateCommand<string>(Reload);
- }
- private void SetSc(string obj)
- {
- NotifiableSCConfigItem item = ScItemList.First(x => x.PathName == obj);
- if (item.Type != SCConfigType.String.ToString() && string.IsNullOrEmpty(item.SetPoint))
- return;
- SC.SetItemValueFromString(obj, item.SetPoint == null ? "" : item.SetPoint);
- item.BoolValue = SC.GetConfigItem(obj).BoolValue;
- item.IntValue = SC.GetConfigItem(obj).IntValue;
- item.StringValue = SC.GetConfigItem(obj).StringValue;
- item.DoubleValue = SC.GetConfigItem(obj).DoubleValue;
- item.InvokePropertyChanged(nameof(item.Value));
- }
- public void Reload(string obj)
- {
- foreach (var item in ScItemList)
- {
- item.BoolValue = SC.GetConfigItem(item.PathName).BoolValue;
- item.IntValue = SC.GetConfigItem(item.PathName).IntValue;
- item.StringValue = SC.GetConfigItem(item.PathName).StringValue;
- item.DoubleValue = SC.GetConfigItem(item.PathName).DoubleValue;
- item.SetPoint = item.Value.ToString();
- item.InvokePropertyChanged(nameof(item.Value));
- item.InvokePropertyChanged(nameof(item.SetPoint));
- }
- }
- public void Init(string module = null)
- {
- if (_scItems != null) return;
- _scItems = SC.GetItemList();
- Load(null);
- }
- public void Load(string module)
- {
- if (_scItems == null) return;
- ScItemList.Clear();
- int i = 0;
- foreach (var scItem in _scItems)
- {
- i++;
- if (!string.IsNullOrWhiteSpace(module) && scItem.Path.Split('.')[0] != module) continue;
- ScItemList.Add(new NotifiableSCConfigItem()
- {
- BoolValue = scItem.BoolValue,
- Default = scItem.Default,
- Description = scItem.Description,
- DoubleValue = scItem.DoubleValue,
- IntValue = scItem.IntValue,
- Max = scItem.Max,
- Min = scItem.Min,
- Name = scItem.Name,
- Type = scItem.Type,
- Tag = scItem.Tag,
- Parameter = scItem.Parameter,
- Path = scItem.Path,
- SetPoint = scItem.Value.ToString(),
- StringValue = scItem.StringValue,
- Index = i,
- });
- }
- }
- }
- /// <summary>
- /// SCConfigView.xaml 的交互逻辑
- /// </summary>
- public partial class BackendSCConfigView : UserControl
- {
- public BackendSCConfigView()
- {
- InitializeComponent();
- this.Loaded += SCConfigView_Loaded;
- this.IsVisibleChanged += BackendSCConfigView_IsVisibleChanged;
- }
- private void BackendSCConfigView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- if (this.DataContext == null)
- {
- DataContext = new BackendSCConfigViewModel();
- }
- (this.DataContext as BackendSCConfigViewModel).Reload(null);
- }
- private void SCConfigView_Loaded(object sender, RoutedEventArgs e)
- {
- if (this.DataContext == null)
- {
- DataContext = new BackendSCConfigViewModel();
- }
- List<string> modules = new List<string>();
- foreach (var item in (this.DataContext as BackendSCConfigViewModel).ScItemList)
- {
- modules.Add(item.Path.Split('.')[0]);
- }
- modules = modules.Distinct().ToList();
- int i = 0;
- modules.ForEach(module =>
- {
- RadioButton radioButton = new RadioButton() { Content = module, Margin = new Thickness(10, 0, 0, 0), FontSize = 15 };
- sp1.Children.Add(radioButton);
- radioButton.Checked += RadioButton_Checked;
- if (i == 0)
- {
- radioButton.IsChecked = true;
- i++;
- }
- });
- }
- private void RadioButton_Checked(object sender, RoutedEventArgs e)
- {
- (this.DataContext as BackendSCConfigViewModel).Load(((sender as RadioButton).Content.ToString()));
- }
- }
- }
|