参数配置界面弹窗添加
This commit is contained in:
139
SettingModule/ViewModels/Dialogs/TCPConfigViewModel.cs
Normal file
139
SettingModule/ViewModels/Dialogs/TCPConfigViewModel.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Input;
|
||||
using UIShare.PubEvent;
|
||||
using UIShare.UIViewModel;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
|
||||
namespace SettingModule.ViewModels.Dialogs
|
||||
{
|
||||
/// <summary>
|
||||
/// TCP 连接配置对话框 VM。
|
||||
/// 通过 DialogParameters 接收宿主 DeviceInfoModel;保存时把副本写回宿主。
|
||||
/// </summary>
|
||||
public class TCPConfigViewModel : DialogViewModelBase
|
||||
{
|
||||
#region 属性
|
||||
|
||||
private string _title = "TCP 连接配置";
|
||||
public string Title
|
||||
{
|
||||
get => _title;
|
||||
set => SetProperty(ref _title, value);
|
||||
}
|
||||
|
||||
/// <summary>编辑用的副本,取消时不会污染宿主对象。</summary>
|
||||
private TcpConnectionConfig _config = new();
|
||||
public TcpConnectionConfig Config
|
||||
{
|
||||
get => _config;
|
||||
set => SetProperty(ref _config, value);
|
||||
}
|
||||
|
||||
/// <summary>常用 Modbus / 自定义协议端口建议。</summary>
|
||||
public ObservableCollection<int> CommonPorts { get; } = new()
|
||||
{
|
||||
502, 102, 80, 8080, 5020, 4840
|
||||
};
|
||||
|
||||
/// <summary>常用超时(毫秒)。</summary>
|
||||
public ObservableCollection<int> CommonTimeouts { get; } = new()
|
||||
{
|
||||
500, 1000, 2000, 3000, 5000, 10000
|
||||
};
|
||||
|
||||
private string _errorMessage = string.Empty;
|
||||
public string ErrorMessage
|
||||
{
|
||||
get => _errorMessage;
|
||||
set => SetProperty(ref _errorMessage, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
public ICommand SaveCommand { get; }
|
||||
public ICommand CancelCommand { get; }
|
||||
#endregion
|
||||
|
||||
// 用于保存时把副本回写到原对象
|
||||
private DeviceInfoModel? _hostDevice;
|
||||
|
||||
public TCPConfigViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
SaveCommand = new DelegateCommand(OnSave);
|
||||
CancelCommand = new DelegateCommand(OnCancel);
|
||||
}
|
||||
|
||||
private bool Validate(out string error)
|
||||
{
|
||||
error = string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(Config.IPAddress))
|
||||
{
|
||||
error = "IP 地址不能为空";
|
||||
return false;
|
||||
}
|
||||
if (!System.Net.IPAddress.TryParse(Config.IPAddress, out _))
|
||||
{
|
||||
error = "IP 地址格式不正确";
|
||||
return false;
|
||||
}
|
||||
if (Config.Port <= 0 || Config.Port > 65535)
|
||||
{
|
||||
error = "端口范围应在 1 - 65535";
|
||||
return false;
|
||||
}
|
||||
if (Config.SendTimeout < 0 || Config.ReceiveTimeout < 0)
|
||||
{
|
||||
error = "超时时间不能为负数";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnSave()
|
||||
{
|
||||
if (!Validate(out var error))
|
||||
{
|
||||
ErrorMessage = error;
|
||||
return;
|
||||
}
|
||||
ErrorMessage = string.Empty;
|
||||
|
||||
// 把副本写回宿主
|
||||
if (_hostDevice != null)
|
||||
{
|
||||
_hostDevice.TcpConfig ??= new TcpConnectionConfig();
|
||||
Config.CopyTo(_hostDevice.TcpConfig);
|
||||
_hostDevice.ConnectionType = "TCP";
|
||||
}
|
||||
|
||||
RequestClose.Invoke(ButtonResult.OK);
|
||||
}
|
||||
|
||||
private void OnCancel() => RequestClose.Invoke(ButtonResult.Cancel);
|
||||
|
||||
#region Prism Dialog 规范
|
||||
public override void OnDialogOpened(IDialogParameters parameters)
|
||||
{
|
||||
_eventAggregator.GetEvent<OverlayEvent>().Publish(true);
|
||||
|
||||
if (parameters.ContainsKey("Device"))
|
||||
{
|
||||
_hostDevice = parameters.GetValue<DeviceInfoModel>("Device");
|
||||
Title = $"TCP 连接配置 - {_hostDevice?.DeviceName}";
|
||||
Config = new TcpConnectionConfig(_hostDevice?.TcpConfig);
|
||||
}
|
||||
else if (parameters.ContainsKey("Config"))
|
||||
{
|
||||
Config = new TcpConnectionConfig(parameters.GetValue<TcpConnectionConfig>("Config"));
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnDialogClosed()
|
||||
{
|
||||
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user