ConnectionView.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Runtime.Serialization;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  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 Aitex.Core.UI.MVVM;
  19. using MECF.Framework.Common.CommonData;
  20. using MECF.Framework.Common.Communications;
  21. using MECF.Framework.Common.IOCore;
  22. namespace EfemRT.Backends
  23. {
  24. /// <summary>
  25. /// ConnectionView.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class ConnectionView : UserControl
  28. {
  29. public ConnectionView()
  30. {
  31. InitializeComponent();
  32. DataContext = new ConnectionViewModel();
  33. this.IsVisibleChanged += IOView_IsVisibleChanged;
  34. }
  35. private void IOView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  36. {
  37. (DataContext as TimerViewModelBase).EnableTimer(IsVisible);
  38. }
  39. }
  40. public class ConnectionViewModel : TimerViewModelBase
  41. {
  42. Random _rd = new Random();
  43. public ObservableCollection<NotifiableConnectionItem> ListConnections { get; set; }
  44. public ICommand ConnectCommand { get; set; }
  45. public ConnectionViewModel() : base(nameof(ConnectionViewModel))
  46. {
  47. ListConnections = new ObservableCollection<NotifiableConnectionItem>();
  48. ConnectCommand = new DelegateCommand<string>(DoConnect);
  49. }
  50. private void DoConnect(string obj)
  51. {
  52. ConnectionManager.Instance.Connect(obj);
  53. }
  54. protected override void Poll()
  55. {
  56. Application.Current.Dispatcher.Invoke(new Action(() =>
  57. {
  58. foreach (var item in ConnectionManager.Instance.ConnectionList)
  59. {
  60. if (ListConnections.Count == 0 || (ListConnections.FirstOrDefault(x => x.Name == item.Name)==null))
  61. {
  62. ListConnections.Add(item);
  63. }
  64. else
  65. {
  66. var find = ListConnections.First(x => x.Name == item.Name);
  67. find.IsConnected = find.IsConnected;
  68. find.InvokePropertyChanged("IsConnected");
  69. }
  70. }
  71. InvokePropertyChanged("ListConnections");
  72. }));
  73. }
  74. }
  75. public class IoButton : ToggleButton
  76. {
  77. public static readonly DependencyProperty ONProperty;
  78. static IoButton()
  79. {
  80. ONProperty = DependencyProperty.Register("ON", typeof(bool), typeof(IoButton));
  81. }
  82. public bool ON
  83. {
  84. get { return (bool)GetValue(ONProperty); }
  85. set { SetValue(ONProperty, value); }
  86. }
  87. }
  88. public class BoolBackgroundConverter : IValueConverter
  89. {
  90. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  91. {
  92. bool? ret = (bool?)value;
  93. return ret.HasValue && ret.Value ? "LightBlue" : "Transparent";
  94. }
  95. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  96. {
  97. return null;
  98. }
  99. }
  100. }