using System; using System.Globalization; using System.Windows.Controls; namespace TeamAAS_VP.ValidationRules { public class TCPPortValidationRule : ValidationRule { public override ValidationResult Validate(object value, CultureInfo cultureInfo) { // 判断输入值是否为空 if (value == null || string.IsNullOrWhiteSpace(value.ToString())) { return new ValidationResult(false, "The TCP port cannot be empty.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t")); } string portString = value.ToString(); if (int.TryParse(portString, out int port)) { // 检查端口号是否在有效范围内 (0-65535) if (port > 0 && port <= 65535) { return ValidationResult.ValidResult; } else { return new ValidationResult(false, "The TCP port must be between 1 and 65535".Replace("\\n", Environment.NewLine).Replace("\\t", "\t")); } } else { return new ValidationResult(false, "Invalid TCP port format.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t")); } } } }