using System.Collections.ObjectModel; using System.Linq; using System.Reactive; using LoraGamepad.Models; using ReactiveUI; namespace LoraGamepad.ViewModels; public class TProViewModel : ViewModelBase { private bool _isOpenPortButtonVisible = true; private bool _isClosePortButtonVisible; private ObservableCollection _portList = new(); public CBPortItem PortSelectItem { get; set; } public ReactiveCommand OpenPort { get; } public ReactiveCommand ClosePort { get; } public TProViewModel() { PortList = CBPortItem.GetPortList(); OpenPort = ReactiveCommand.Create(PortOpenButtonClick); ClosePort = ReactiveCommand.Create(PortCloseButtonClick); } /// /// ComboBox串口列表绑定对象 /// public ObservableCollection PortList { get => _portList; set => this.RaiseAndSetIfChanged(ref _portList, value); } /// /// 刷新串口列表事件处理函数 /// public void OnPortListComboBoxPressed() { var newList = CBPortItem.GetPortList(); PortList.Except(newList,new CBPortItemCompare()).ToList().ForEach(a => { PortList.Remove(a); }); newList.Except(PortList, new CBPortItemCompare()).ToList().ForEach(a => { PortList.Add(a); }); newList.Except(PortList, new CBPortItemCompare()).ToList().ForEach(a => { PortList.Add(a); }); } /// /// 关闭串口按键可见性属性 /// public bool IsClosePortButtonVisible { get => _isClosePortButtonVisible; set => this.RaiseAndSetIfChanged(ref _isClosePortButtonVisible, value); } /// /// 打开串口按键可见性属性 /// public bool IsOpenPortButtonVisible { get => _isOpenPortButtonVisible; set => this.RaiseAndSetIfChanged(ref _isOpenPortButtonVisible, value); } /// /// 打开串口按键响应 /// private void PortOpenButtonClick() { if (PortSelectItem.PortName == "") return; SerialPort.Me.Open(PortSelectItem.PortName,420000,1000); IsOpenPortButtonVisible = false; IsClosePortButtonVisible = true; } /// /// 关闭串口按键响应 /// private void PortCloseButtonClick() { SerialPort.Me.Close(); IsOpenPortButtonVisible = true; IsClosePortButtonVisible = false; } }