TProPCMonitor/LoraGamepad/ViewModels/TProViewModel.cs

88 lines
2.5 KiB
C#
Raw Normal View History

2022-11-06 18:44:23 +08:00
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using LoraGamepad.Models;
using ReactiveUI;
2022-11-06 14:49:30 +08:00
namespace LoraGamepad.ViewModels;
public class TProViewModel : ViewModelBase
{
2022-11-06 18:44:23 +08:00
private bool _isOpenPortButtonVisible = true;
private bool _isClosePortButtonVisible;
private ObservableCollection<CBPortItem> _portList = new();
public CBPortItem PortSelectItem { get; set; }
public ReactiveCommand<Unit, Unit> OpenPort { get; }
public ReactiveCommand<Unit, Unit> ClosePort { get; }
2022-11-06 14:49:30 +08:00
public TProViewModel()
{
2022-11-06 18:44:23 +08:00
PortList = CBPortItem.GetPortList();
2022-11-06 14:49:30 +08:00
2022-11-06 18:44:23 +08:00
OpenPort = ReactiveCommand.Create(PortOpenButtonClick);
ClosePort = ReactiveCommand.Create(PortCloseButtonClick);
}
/// <summary>
/// ComboBox串口列表绑定对象
/// </summary>
public ObservableCollection<CBPortItem> PortList
{
get => _portList;
set => this.RaiseAndSetIfChanged(ref _portList, value);
}
/// <summary>
/// 刷新串口列表事件处理函数
/// </summary>
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); });
}
/// <summary>
/// 关闭串口按键可见性属性
/// </summary>
public bool IsClosePortButtonVisible
{
get => _isClosePortButtonVisible;
set => this.RaiseAndSetIfChanged(ref _isClosePortButtonVisible, value);
2022-11-06 14:49:30 +08:00
}
2022-11-06 18:44:23 +08:00
/// <summary>
/// 打开串口按键可见性属性
/// </summary>
public bool IsOpenPortButtonVisible
{
get => _isOpenPortButtonVisible;
set => this.RaiseAndSetIfChanged(ref _isOpenPortButtonVisible, value);
}
/// <summary>
/// 打开串口按键响应
/// </summary>
private void PortOpenButtonClick()
{
if (PortSelectItem.PortName == "") return;
SerialPort.Me.Open(PortSelectItem.PortName,420000,1000);
IsOpenPortButtonVisible = false;
IsClosePortButtonVisible = true;
}
/// <summary>
/// 关闭串口按键响应
/// </summary>
private void PortCloseButtonClick()
{
SerialPort.Me.Close();
IsOpenPortButtonVisible = true;
IsClosePortButtonVisible = false;
}
2022-11-06 14:49:30 +08:00
}