BackendSCConfigView.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. public 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. Init();
  59. SetScCommand = new DelegateCommand<string>(SetSc);
  60. ReloadCommand = new DelegateCommand<string>(Reload);
  61. _scItems = SC.GetItemList();
  62. ScItemList = new ObservableCollection<NotifiableSCConfigItem>();
  63. }
  64. private void SetSc(string obj)
  65. {
  66. NotifiableSCConfigItem item = ScItemList.First(x => x.PathName == obj);
  67. if (item.Type != SCConfigType.String.ToString() && string.IsNullOrEmpty(item.SetPoint))
  68. return;
  69. SC.SetItemValueFromString(obj, item.SetPoint == null ? "" : item.SetPoint);
  70. item.BoolValue = SC.GetConfigItem(obj).BoolValue;
  71. item.IntValue = SC.GetConfigItem(obj).IntValue;
  72. item.StringValue = SC.GetConfigItem(obj).StringValue;
  73. item.DoubleValue = SC.GetConfigItem(obj).DoubleValue;
  74. item.InvokePropertyChanged(nameof(item.Value));
  75. }
  76. public void Reload(string obj)
  77. {
  78. //foreach (var item in ScItemList)
  79. //{
  80. // item.BoolValue = SC.GetConfigItem(item.PathName).BoolValue;
  81. // item.IntValue = SC.GetConfigItem(item.PathName).IntValue;
  82. // item.StringValue = SC.GetConfigItem(item.PathName).StringValue;
  83. // item.DoubleValue = SC.GetConfigItem(item.PathName).DoubleValue;
  84. // item.InvokePropertyChanged(nameof(item.Value));
  85. //}
  86. }
  87. void Init()
  88. {
  89. //if (_scItems != null) return;
  90. //{
  91. // _scItems = SC.GetItemList();
  92. // ScItemList = new ObservableCollection<NotifiableSCConfigItem>();
  93. // int i = 0;
  94. // foreach (var scItem in _scItems)
  95. // {
  96. // i++;
  97. // ScItemList.Add(new NotifiableSCConfigItem()
  98. // {
  99. // BoolValue = scItem.BoolValue,
  100. // Default = scItem.Default,
  101. // Description = scItem.Description,
  102. // DoubleValue = scItem.DoubleValue,
  103. // IntValue = scItem.IntValue,
  104. // Max = scItem.Max,
  105. // Min = scItem.Min,
  106. // Name = scItem.Name,
  107. // Type = scItem.Type,
  108. // Tag = scItem.Tag,
  109. // Parameter = scItem.Parameter,
  110. // Path = scItem.Path,
  111. // SetPoint = scItem.Value.ToString(),
  112. // StringValue = scItem.StringValue,
  113. // Index = i,
  114. // });
  115. // }
  116. //}
  117. }
  118. public void partLoad(string module)
  119. {
  120. if (_scItems == null) return;
  121. {
  122. ScItemList.Clear();
  123. int i = 0;
  124. foreach (var scItem in _scItems)
  125. {
  126. i++;
  127. if (scItem.Path.Split('.')[0] != module)
  128. {
  129. continue;
  130. }
  131. ScItemList.Add(new NotifiableSCConfigItem()
  132. {
  133. BoolValue = scItem.BoolValue,
  134. Default = scItem.Default,
  135. Description = scItem.Description,
  136. DoubleValue = scItem.DoubleValue,
  137. IntValue = scItem.IntValue,
  138. Max = scItem.Max,
  139. Min = scItem.Min,
  140. Name = scItem.Name,
  141. Type = scItem.Type,
  142. Tag = scItem.Tag,
  143. Parameter = scItem.Parameter,
  144. Path = scItem.Path,
  145. SetPoint = scItem.Value.ToString(),
  146. StringValue = scItem.StringValue,
  147. Index = i,
  148. });
  149. }
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// SCConfigView.xaml 的交互逻辑
  155. /// </summary>
  156. public partial class BackendSCConfigView : UserControl
  157. {
  158. public BackendSCConfigView()
  159. {
  160. InitializeComponent();
  161. this.Loaded += SCConfigView_Loaded;
  162. this.IsVisibleChanged += BackendSCConfigView_IsVisibleChanged;
  163. }
  164. private void BackendSCConfigView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  165. {
  166. if (this.DataContext == null)
  167. {
  168. DataContext = new BackendSCConfigViewModel();
  169. }
  170. (this.DataContext as BackendSCConfigViewModel).Reload(null);
  171. }
  172. private void SCConfigView_Loaded(object sender, RoutedEventArgs e)
  173. {
  174. if (this.DataContext == null)
  175. {
  176. DataContext = new BackendSCConfigViewModel();
  177. }
  178. List<string> modules = new List<string>();
  179. foreach (var item in (this.DataContext as BackendSCConfigViewModel)._scItems)
  180. {
  181. modules.Add(item.Path.Split('.')[0]);
  182. }
  183. modules = modules.Distinct().ToList();
  184. int i = 0;
  185. modules.ForEach(module =>
  186. {
  187. RadioButton radioButton = new RadioButton();
  188. sp1.Children.Add(
  189. radioButton = new RadioButton() { Content = module, Margin = new Thickness(10, 0, 0, 0), FontSize = 15 }
  190. );
  191. radioButton.Checked += RadioButton_Checked;
  192. if (i == 0)
  193. {
  194. radioButton.IsChecked = true;
  195. i++;
  196. }
  197. });
  198. }
  199. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  200. {
  201. (this.DataContext as BackendSCConfigViewModel).partLoad(((sender as RadioButton).Content.ToString()));
  202. }
  203. }
  204. }