优化更新代码,添加界面功能并整合
This commit is contained in:
22
ShrlAlgo.Toolkit.Mvvm/Attributes/GreaterThanAttribute.cs
Normal file
22
ShrlAlgo.Toolkit.Mvvm/Attributes/GreaterThanAttribute.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Mvvm.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
|
||||
public sealed class GreaterThanAttribute : ValidationAttribute
|
||||
{
|
||||
public GreaterThanAttribute(string propertyName)
|
||||
{
|
||||
PropertyName = propertyName;
|
||||
}
|
||||
|
||||
public string PropertyName { get; }
|
||||
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
object instance = validationContext.ObjectInstance,
|
||||
otherValue = instance.GetType().GetProperty(PropertyName).GetValue(instance);
|
||||
|
||||
return ((IComparable)value).CompareTo(otherValue) > 0 ? ValidationResult.Success : new($"当前值小于{value}");
|
||||
}
|
||||
}
|
||||
12
ShrlAlgo.Toolkit.Mvvm/Attributes/IsNumericAttribute.cs
Normal file
12
ShrlAlgo.Toolkit.Mvvm/Attributes/IsNumericAttribute.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Mvvm.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
|
||||
public sealed class IsNumericAttribute : ValidationAttribute
|
||||
{
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
return double.TryParse(value.ToString(), out var _) ? ValidationResult.Success : new("输入内容不是数值。");
|
||||
}
|
||||
}
|
||||
19
ShrlAlgo.Toolkit.Mvvm/Attributes/MaximumAttribute.cs
Normal file
19
ShrlAlgo.Toolkit.Mvvm/Attributes/MaximumAttribute.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Mvvm.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
|
||||
public sealed class MaximumAttribute : ValidationAttribute
|
||||
{
|
||||
public MaximumAttribute(double maxValue = 0)
|
||||
{
|
||||
MaxValue = maxValue;
|
||||
}
|
||||
|
||||
public double MaxValue { get; }
|
||||
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
return double.TryParse(value.ToString(), out var d) && d <= MaxValue ? ValidationResult.Success : new ValidationResult("输入值超出范围");
|
||||
}
|
||||
}
|
||||
19
ShrlAlgo.Toolkit.Mvvm/Attributes/MinimumAttribute.cs
Normal file
19
ShrlAlgo.Toolkit.Mvvm/Attributes/MinimumAttribute.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Mvvm.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
|
||||
public sealed class MinimumAttribute : ValidationAttribute
|
||||
{
|
||||
public MinimumAttribute(double minValue = 0)
|
||||
{
|
||||
MinValue = minValue;
|
||||
}
|
||||
|
||||
public double MinValue { get; }
|
||||
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
return double.TryParse(value.ToString(), out var d) && d >= MinValue ? ValidationResult.Success : new ValidationResult("<22><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ");
|
||||
}
|
||||
}
|
||||
13
ShrlAlgo.Toolkit.Mvvm/Attributes/NotNullOrEmptyAttribute.cs
Normal file
13
ShrlAlgo.Toolkit.Mvvm/Attributes/NotNullOrEmptyAttribute.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Mvvm.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
|
||||
public sealed class NotNullOrEmptyAttribute : ValidationAttribute
|
||||
{
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
var b = value != null || (value is string str && !string.IsNullOrEmpty(str));
|
||||
return b ? ValidationResult.Success : new("输入内容不是数值。");
|
||||
}
|
||||
}
|
||||
31
ShrlAlgo.Toolkit.Mvvm/Attributes/UndefinedCharAttribute.cs
Normal file
31
ShrlAlgo.Toolkit.Mvvm/Attributes/UndefinedCharAttribute.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Mvvm.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
|
||||
public class UndefinedCharAttribute : ValidationAttribute
|
||||
{
|
||||
public UndefinedCharAttribute(params char[] chars)
|
||||
{
|
||||
Chars = chars;
|
||||
}
|
||||
|
||||
public char[] Chars { get; }
|
||||
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
//char[] chars = new[] { '\\', ':', '{', '}', '[', ']', '|', ';', '<', '>', '?', '`', '~' };
|
||||
if (value != null)
|
||||
{
|
||||
foreach (var c in Chars)
|
||||
{
|
||||
if (value.ToString().IndexOf(c) > -1)
|
||||
{
|
||||
return new ValidationResult($"输入字符不合法,名称不能包含字符:'\\ : [ ] | {{}}; < > ? ` ~");
|
||||
}
|
||||
}
|
||||
}
|
||||
return ValidationResult.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user