完成添加变量对话框验证
This commit is contained in:
@@ -13,9 +13,9 @@ public partial class MqttSelectionDialogViewModel : ObservableObject
|
||||
// [ObservableProperty]
|
||||
// private Mqtt? selectedMqtt;
|
||||
//
|
||||
// public MqttSelectionDialogViewModel(DataServices dataServices)
|
||||
// public MqttSelectionDialogViewModel(DataServices _dataServices)
|
||||
// {
|
||||
// Mqtts = new ObservableCollection<Mqtt>(dataServices.Mqtts);
|
||||
// Mqtts = new ObservableCollection<Mqtt>(_dataServices.Mqtts);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
using AutoMapper;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Helper;
|
||||
using DMS.WPF.Services;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
|
||||
namespace DMS.WPF.ViewModels.Dialogs;
|
||||
@@ -9,26 +14,97 @@ public partial class VariableDialogViewModel : DialogViewModelBase<VariableItemV
|
||||
[ObservableProperty]
|
||||
private VariableItemViewModel _variable;
|
||||
|
||||
public VariableDialogViewModel(string title,string primaryButText,VariableItemViewModel variable=null)
|
||||
[ObservableProperty]
|
||||
private string _errorMessage = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _hasError;
|
||||
private readonly DataServices _dataServices;
|
||||
private readonly IVariableAppService _variableAppService;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public VariableDialogViewModel(DataServices dataServices, IVariableAppService variableAppService, IMapper mapper)
|
||||
{
|
||||
if (variable==null)
|
||||
{
|
||||
Variable=new VariableItemViewModel();
|
||||
}
|
||||
else
|
||||
{
|
||||
Variable=variable;
|
||||
}
|
||||
|
||||
Title=title;
|
||||
PrimaryButText=primaryButText;
|
||||
|
||||
Variable = new VariableItemViewModel();
|
||||
|
||||
this._dataServices = dataServices;
|
||||
this._variableAppService = variableAppService;
|
||||
this._mapper = mapper;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void PrimaryButton()
|
||||
|
||||
public async Task<bool> PrimaryButtonAsync()
|
||||
{
|
||||
Close(Variable);
|
||||
// 清除之前的错误信息
|
||||
ErrorMessage = string.Empty;
|
||||
HasError = false;
|
||||
// 验证输入
|
||||
bool isValid = ValidateInputAsync();
|
||||
if (isValid)
|
||||
{
|
||||
Close(Variable);
|
||||
}
|
||||
|
||||
return isValid;
|
||||
}
|
||||
|
||||
private bool ValidateInputAsync()
|
||||
{
|
||||
// 检查变量名称是否为空
|
||||
if (string.IsNullOrWhiteSpace(Variable.Name))
|
||||
{
|
||||
ErrorMessage = "变量名称不能为空";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 根据协议类型检查地址
|
||||
if (Variable.Protocol == Core.Enums.ProtocolType.S7 &&
|
||||
string.IsNullOrWhiteSpace(Variable.S7Address))
|
||||
{
|
||||
ErrorMessage = "S7协议必须填写S7地址";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Variable.Protocol == Core.Enums.ProtocolType.OpcUa &&
|
||||
string.IsNullOrWhiteSpace(Variable.OpcUaNodeId))
|
||||
{
|
||||
ErrorMessage = "OPC UA协议必须填写节点ID";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
//检查变量是否存在
|
||||
var existVariable = _dataServices.Variables.FirstOrDefault(v => v.Name == Variable.Name || v.S7Address == Variable.S7Address || v.OpcUaNodeId == Variable.OpcUaNodeId);
|
||||
if (existVariable != null)
|
||||
{
|
||||
|
||||
if (Variable.Name == existVariable.Name)
|
||||
{
|
||||
ErrorMessage = $"变量名称:{existVariable.Name}已经存在。";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Variable.Protocol == Core.Enums.ProtocolType.S7 && Variable.S7Address == existVariable.S7Address)
|
||||
{
|
||||
ErrorMessage = $"变量S7地址:{existVariable.S7Address}已经存在。";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Variable.Protocol == Core.Enums.ProtocolType.OpcUa && Variable.OpcUaNodeId == existVariable.OpcUaNodeId)
|
||||
{
|
||||
ErrorMessage = $"变量OpcUa节点ID:{existVariable.OpcUaNodeId}已经存在。";
|
||||
HasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void CancleButton()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user