BackendSCConfigView.xaml.cs 7.0 KB

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