AflexControl.xaml.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using Prism.Commands;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using Team.FFFeederService;
  19. using Team.FFFeederService.Interfaces;
  20. using TeamAAS_VP;
  21. namespace TeamAAS_VP.Controls
  22. {
  23. /// <summary>
  24. /// AflexControl.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class AflexControl : UserControl, INotifyPropertyChanged
  27. {
  28. public AflexControl()
  29. {
  30. InitializeComponent();
  31. this.DataContext = this;
  32. }
  33. public AfagFeeder Feeder { get; set; }
  34. private int _ConfigIndex;
  35. /// <summary>
  36. /// 配置编号
  37. /// </summary>
  38. public int ConfigIndex
  39. {
  40. get { return _ConfigIndex; }
  41. set { SetProperty(ref _ConfigIndex, value); }
  42. }
  43. private double _Intensity;
  44. /// <summary>
  45. /// 振动强度
  46. /// </summary>
  47. public double Intensity
  48. {
  49. get { return _Intensity; }
  50. set { SetProperty(ref _Intensity, value); }
  51. }
  52. private double _Direction;
  53. /// <summary>
  54. /// 方向
  55. /// </summary>
  56. public double Direction
  57. {
  58. get { return _Direction; }
  59. set { SetProperty(ref _Direction, value); }
  60. }
  61. private double _Backlight;
  62. /// <summary>
  63. /// 背光亮度
  64. /// </summary>
  65. public double Backlight
  66. {
  67. get { return _Backlight; }
  68. set { SetProperty(ref _Backlight, value); }
  69. }
  70. private DelegateCommand<string> _ActionCommand;
  71. public DelegateCommand<string> ActionCommand =>
  72. _ActionCommand ?? (_ActionCommand = new DelegateCommand<string>(ExecuteActionCommand));
  73. /// <summary>
  74. /// 执行命令
  75. /// </summary>
  76. /// <param name="cmd"></param>
  77. void ExecuteActionCommand(string cmd)
  78. {
  79. try
  80. {
  81. if (Feeder == null) { return; }
  82. switch (int.Parse(cmd))
  83. {
  84. case 1:
  85. if (ConfigIndex<0) { return; }
  86. Feeder.LoadConfiguration(ConfigIndex+1);
  87. break;
  88. case 2:
  89. Feeder.SetIntensity(Intensity);
  90. break;
  91. case 3:
  92. Feeder.SetDirection(Direction);
  93. break;
  94. case 4:
  95. Feeder.SetBackLight(Backlight);
  96. break;
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. LogHelper.WriteLogError("Afag Feeder写入参数时出错!", ex);
  102. }
  103. }
  104. #region 属性更改通知
  105. /// <summary>
  106. /// Occurs when a property value changes.
  107. /// </summary>
  108. public event PropertyChangedEventHandler PropertyChanged;
  109. /// <summary>
  110. /// Checks if a property already matches a desired value. Sets the property and
  111. /// notifies listeners only when necessary.
  112. /// </summary>
  113. /// <typeparam name="T">Type of the property.</typeparam>
  114. /// <param name="storage">Reference to a property with both getter and setter.</param>
  115. /// <param name="value">Desired value for the property.</param>
  116. /// <param name="propertyName">Name of the property used to notify listeners. This
  117. /// value is optional and can be provided automatically when invoked from compilers that
  118. /// support CallerMemberName.</param>
  119. /// <returns>True if the value was changed, false if the existing value matched the
  120. /// desired value.</returns>
  121. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  122. {
  123. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  124. storage = value;
  125. RaisePropertyChanged(propertyName);
  126. return true;
  127. }
  128. /// <summary>
  129. /// Checks if a property already matches a desired value. Sets the property and
  130. /// notifies listeners only when necessary.
  131. /// </summary>
  132. /// <typeparam name="T">Type of the property.</typeparam>
  133. /// <param name="storage">Reference to a property with both getter and setter.</param>
  134. /// <param name="value">Desired value for the property.</param>
  135. /// <param name="propertyName">Name of the property used to notify listeners. This
  136. /// value is optional and can be provided automatically when invoked from compilers that
  137. /// support CallerMemberName.</param>
  138. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  139. /// <returns>True if the value was changed, false if the existing value matched the
  140. /// desired value.</returns>
  141. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  142. {
  143. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  144. storage = value;
  145. onChanged?.Invoke();
  146. RaisePropertyChanged(propertyName);
  147. return true;
  148. }
  149. /// <summary>
  150. /// Raises this object's PropertyChanged event.
  151. /// </summary>
  152. /// <param name="propertyName">Name of the property used to notify listeners. This
  153. /// value is optional and can be provided automatically when invoked from compilers
  154. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  155. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  156. {
  157. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  158. }
  159. /// <summary>
  160. /// Raises this object's PropertyChanged event.
  161. /// </summary>
  162. /// <param name="args">The PropertyChangedEventArgs</param>
  163. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  164. {
  165. PropertyChanged?.Invoke(this, args);
  166. }
  167. #endregion
  168. }
  169. }