BackendSCConfigView.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. Init();
  59. SetScCommand = new DelegateCommand<string>(SetSc);
  60. ReloadCommand = new DelegateCommand<string>(Reload);
  61. }
  62. private void SetSc(string obj)
  63. {
  64. NotifiableSCConfigItem item = ScItemList.First(x => x.PathName == obj);
  65. if (item.Type != SCConfigType.String.ToString() && string.IsNullOrEmpty(item.SetPoint))
  66. return;
  67. SC.SetItemValueFromString(obj, item.SetPoint == null ? "" : item.SetPoint);
  68. item.BoolValue = SC.GetConfigItem(obj).BoolValue;
  69. item.IntValue = SC.GetConfigItem(obj).IntValue;
  70. item.StringValue = SC.GetConfigItem(obj).StringValue;
  71. item.DoubleValue = SC.GetConfigItem(obj).DoubleValue;
  72. item.InvokePropertyChanged(nameof(item.Value));
  73. }
  74. public void Reload(string obj)
  75. {
  76. foreach (var item in ScItemList)
  77. {
  78. item.BoolValue = SC.GetConfigItem(item.PathName).BoolValue;
  79. item.IntValue = SC.GetConfigItem(item.PathName).IntValue;
  80. item.StringValue = SC.GetConfigItem(item.PathName).StringValue;
  81. item.DoubleValue = SC.GetConfigItem(item.PathName).DoubleValue;
  82. item.InvokePropertyChanged(nameof(item.Value));
  83. }
  84. }
  85. void Init()
  86. {
  87. if (_scItems != null) return;
  88. {
  89. _scItems = SC.GetItemList();
  90. ScItemList = new ObservableCollection<NotifiableSCConfigItem>();
  91. int i = 0;
  92. foreach (var scItem in _scItems)
  93. {
  94. i++;
  95. ScItemList.Add(new NotifiableSCConfigItem()
  96. {
  97. BoolValue = scItem.BoolValue,
  98. Default = scItem.Default,
  99. Description = scItem.Description,
  100. DoubleValue = scItem.DoubleValue,
  101. IntValue = scItem.IntValue,
  102. Max = scItem.Max,
  103. Min = scItem.Min,
  104. Name = scItem.Name,
  105. Type = scItem.Type,
  106. Tag = scItem.Tag,
  107. Parameter = scItem.Parameter,
  108. Path = scItem.Path,
  109. SetPoint = scItem.Value.ToString(),
  110. StringValue = scItem.StringValue,
  111. Index = i,
  112. });
  113. }
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// SCConfigView.xaml 的交互逻辑
  119. /// </summary>
  120. public partial class BackendSCConfigView : UserControl
  121. {
  122. public BackendSCConfigView()
  123. {
  124. InitializeComponent();
  125. this.Loaded += SCConfigView_Loaded;
  126. this.IsVisibleChanged += BackendSCConfigView_IsVisibleChanged;
  127. }
  128. private void BackendSCConfigView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  129. {
  130. if (this.DataContext == null)
  131. {
  132. DataContext = new BackendSCConfigViewModel();
  133. }
  134. (this.DataContext as BackendSCConfigViewModel).Reload(null);
  135. }
  136. private void SCConfigView_Loaded(object sender, RoutedEventArgs e)
  137. {
  138. if (this.DataContext == null)
  139. {
  140. DataContext = new BackendSCConfigViewModel();
  141. }
  142. }
  143. }
  144. }