BackendSCConfigView.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Input;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Core.UI.MVVM;
  11. namespace MECF.Framework.RT.Core.Backend
  12. {
  13. public class NotifiableSCConfigItem : SCConfigItem, INotifyPropertyChanged
  14. {
  15. public string SetPoint { get; set; }
  16. public event PropertyChangedEventHandler PropertyChanged;
  17. public void InvokePropertyChanged(string propertyName)
  18. {
  19. if (PropertyChanged != null)
  20. {
  21. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  22. }
  23. }
  24. public void InvokePropertyChanged()
  25. {
  26. PropertyInfo[] ps = this.GetType().GetProperties();
  27. foreach (PropertyInfo p in ps)
  28. {
  29. InvokePropertyChanged(p.Name);
  30. if (p.PropertyType == typeof(ICommand))
  31. {
  32. DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;
  33. if (cmd != null)
  34. cmd.RaiseCanExecuteChanged();
  35. }
  36. }
  37. FieldInfo[] fi = this.GetType().GetFields();
  38. foreach (FieldInfo p in fi)
  39. {
  40. InvokePropertyChanged(p.Name);
  41. if (p.FieldType == typeof(ICommand))
  42. {
  43. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  44. if (cmd != null)
  45. cmd.RaiseCanExecuteChanged();
  46. }
  47. }
  48. }
  49. }
  50. public class BackendSCConfigViewModel : ViewModelBase
  51. {
  52. private List<SCConfigItem> _scItems;
  53. public ObservableCollection<NotifiableSCConfigItem> ScItemList { get; set; }
  54. public ICommand SetScCommand { get; set; }
  55. public ICommand ReloadCommand { get; set; }
  56. public BackendSCConfigViewModel()
  57. {
  58. ScItemList = new ObservableCollection<NotifiableSCConfigItem>();
  59. Init();
  60. SetScCommand = new DelegateCommand<string>(SetSc);
  61. ReloadCommand = new DelegateCommand<string>(Reload);
  62. }
  63. private void SetSc(string obj)
  64. {
  65. NotifiableSCConfigItem item = ScItemList.First(x => x.PathName == obj);
  66. if (item.Type != SCConfigType.String.ToString() && string.IsNullOrEmpty(item.SetPoint))
  67. return;
  68. SC.SetItemValueFromString(obj, item.SetPoint == null ? "" : item.SetPoint);
  69. item.BoolValue = SC.GetConfigItem(obj).BoolValue;
  70. item.IntValue = SC.GetConfigItem(obj).IntValue;
  71. item.StringValue = SC.GetConfigItem(obj).StringValue;
  72. item.DoubleValue = SC.GetConfigItem(obj).DoubleValue;
  73. item.InvokePropertyChanged(nameof(item.Value));
  74. }
  75. public void Reload(string obj)
  76. {
  77. foreach (var item in ScItemList)
  78. {
  79. item.BoolValue = SC.GetConfigItem(item.PathName).BoolValue;
  80. item.IntValue = SC.GetConfigItem(item.PathName).IntValue;
  81. item.StringValue = SC.GetConfigItem(item.PathName).StringValue;
  82. item.DoubleValue = SC.GetConfigItem(item.PathName).DoubleValue;
  83. item.SetPoint = item.Value.ToString();
  84. item.InvokePropertyChanged(nameof(item.Value));
  85. item.InvokePropertyChanged(nameof(item.SetPoint));
  86. }
  87. }
  88. public void Init(string module = null)
  89. {
  90. if (_scItems != null) return;
  91. _scItems = SC.GetItemList();
  92. Load(null);
  93. }
  94. public void Load(string module)
  95. {
  96. if (_scItems == null) return;
  97. ScItemList.Clear();
  98. int i = 0;
  99. foreach (var scItem in _scItems)
  100. {
  101. i++;
  102. if (!string.IsNullOrWhiteSpace(module) && scItem.Path.Split('.')[0] != module) continue;
  103. ScItemList.Add(new NotifiableSCConfigItem()
  104. {
  105. BoolValue = scItem.BoolValue,
  106. Default = scItem.Default,
  107. Description = scItem.Description,
  108. DoubleValue = scItem.DoubleValue,
  109. IntValue = scItem.IntValue,
  110. Max = scItem.Max,
  111. Min = scItem.Min,
  112. Name = scItem.Name,
  113. Type = scItem.Type,
  114. Tag = scItem.Tag,
  115. Parameter = scItem.Parameter,
  116. Path = scItem.Path,
  117. SetPoint = scItem.Value.ToString(),
  118. StringValue = scItem.StringValue,
  119. Index = i,
  120. });
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// SCConfigView.xaml 的交互逻辑
  126. /// </summary>
  127. public partial class BackendSCConfigView : UserControl
  128. {
  129. public BackendSCConfigView()
  130. {
  131. InitializeComponent();
  132. this.Loaded += SCConfigView_Loaded;
  133. this.IsVisibleChanged += BackendSCConfigView_IsVisibleChanged;
  134. }
  135. private void BackendSCConfigView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  136. {
  137. if (this.DataContext == null)
  138. {
  139. DataContext = new BackendSCConfigViewModel();
  140. }
  141. (this.DataContext as BackendSCConfigViewModel).Reload(null);
  142. }
  143. private void SCConfigView_Loaded(object sender, RoutedEventArgs e)
  144. {
  145. if (this.DataContext == null)
  146. {
  147. DataContext = new BackendSCConfigViewModel();
  148. }
  149. List<string> modules = new List<string>();
  150. foreach (var item in (this.DataContext as BackendSCConfigViewModel).ScItemList)
  151. {
  152. modules.Add(item.Path.Split('.')[0]);
  153. }
  154. modules = modules.Distinct().ToList();
  155. int i = 0;
  156. modules.ForEach(module =>
  157. {
  158. RadioButton radioButton = new RadioButton() { Content = module, Margin = new Thickness(10, 0, 0, 0), FontSize = 15 };
  159. sp1.Children.Add(radioButton);
  160. radioButton.Checked += RadioButton_Checked;
  161. if (i == 0)
  162. {
  163. radioButton.IsChecked = true;
  164. i++;
  165. }
  166. });
  167. }
  168. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  169. {
  170. (this.DataContext as BackendSCConfigViewModel).Load(((sender as RadioButton).Content.ToString()));
  171. }
  172. }
  173. }