using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Aitex.Core.UI.MVVM; using MECF.Framework.Common.CommonData; using MECF.Framework.Common.Communications; using MECF.Framework.Common.IOCore; namespace EfemRT.Backends { /// /// ConnectionView.xaml 的交互逻辑 /// public partial class ConnectionView : UserControl { public ConnectionView() { InitializeComponent(); DataContext = new ConnectionViewModel(); this.IsVisibleChanged += IOView_IsVisibleChanged; } private void IOView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { (DataContext as TimerViewModelBase).EnableTimer(IsVisible); } } public class ConnectionViewModel : TimerViewModelBase { Random _rd = new Random(); public ObservableCollection ListConnections { get; set; } public ICommand ConnectCommand { get; set; } public ConnectionViewModel() : base(nameof(ConnectionViewModel)) { ListConnections = new ObservableCollection(); ConnectCommand = new DelegateCommand(DoConnect); } private void DoConnect(string obj) { ConnectionManager.Instance.Connect(obj); } protected override void Poll() { Application.Current.Dispatcher.Invoke(new Action(() => { foreach (var item in ConnectionManager.Instance.ConnectionList) { if (ListConnections.Count == 0 || (ListConnections.FirstOrDefault(x => x.Name == item.Name)==null)) { ListConnections.Add(item); } else { var find = ListConnections.First(x => x.Name == item.Name); find.IsConnected = find.IsConnected; find.InvokePropertyChanged("IsConnected"); } } InvokePropertyChanged("ListConnections"); })); } } public class IoButton : ToggleButton { public static readonly DependencyProperty ONProperty; static IoButton() { ONProperty = DependencyProperty.Register("ON", typeof(bool), typeof(IoButton)); } public bool ON { get { return (bool)GetValue(ONProperty); } set { SetValue(ONProperty, value); } } } public class BoolBackgroundConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { bool? ret = (bool?)value; return ret.HasValue && ret.Value ? "LightBlue" : "Transparent"; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; } } }