MultiValidationBehavior

MultiValidationBehavior 是一种 Behavior,它允许用户组合多个验证程序来根据指定的参数验证文本输入。 例如,可以根据提供的文本输入是否有效来为 Entry 控件采用不同的样式。 通过允许用户将多个现有验证程序链接在一起,它可以在验证时帮助实现高程度的自定义。

重要

.NET MAUI 社区工具包行为不会设置行为的 BindingContext,因为行为可以通过样式共享,并应用于多个控件。 有关详细信息,请参阅 .NET MAUI 行为

语法

以下示例演示如何将 MultiValidationBehavior 添加到 Entry并纳入 4 种不同的验证行为,从而强制实施密码策略。

XAML

包括 XAML 命名空间

若要在 XAML 中使用工具包,需要将以下 xmlns 添加到页面或视图中:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

因此,以下内容:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

</ContentPage>

将被修改为包括 xmlns,如下所示:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

</ContentPage>

使用 MultiValidationBehavior

可以在 XAML 中按如下所示方式使用 MultiValidationBehavior

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="CommunityToolkit.Maui.Sample.Pages.Behaviors.MultiValidationBehaviorPage">

    <ContentPage.Resources>
        <Style x:Key="InvalidEntryStyle" TargetType="Entry">
            <Setter Property="TextColor" Value="Red" />
        </Style>
        <Style x:Key="ValidEntryStyle" TargetType="Entry">
            <Setter Property="TextColor" Value="Green" />
        </Style>
    </ContentPage.Resources>

    <Entry 
        IsPassword="True"
        Placeholder="Password">

        <Entry.Behaviors>
            <toolkit:MultiValidationBehavior 
                InvalidStyle="{StaticResource InvalidEntryStyle}"  
                ValidStyle="{StaticResource ValidEntryStyle}"
                Flags="ValidateOnValueChanged">

                <toolkit:CharactersValidationBehavior 
                    x:Name="DigitValidation" 
                    CharacterType="Digit" 
                    MinimumCharacterTypeCount="1" 
                    toolkit:MultiValidationBehavior.Error="1 digit" 
                    RegexPattern="" />

                <toolkit:CharactersValidationBehavior 
                    x:Name="UpperValidation" 
                    CharacterType="UppercaseLetter" 
                    MinimumCharacterTypeCount="1" 
                    toolkit:MultiValidationBehavior.Error="1 upper case" 
                    RegexPattern="" />

                <toolkit:CharactersValidationBehavior 
                    x:Name="SymbolValidation" 
                    CharacterType="NonAlphanumericSymbol" 
                    MinimumCharacterTypeCount="1" 
                    toolkit:MultiValidationBehavior.Error="1 symbol" 
                    RegexPattern=""  />

                <toolkit:CharactersValidationBehavior 
                    x:Name="AnyValidation" 
                    CharacterType="Any" 
                    MinimumCharacterTypeCount="8" 
                    toolkit:MultiValidationBehavior.Error="8 characters" 
                    RegexPattern="" />
            </toolkit:MultiValidationBehavior>
        </Entry.Behaviors>
    </Entry>

</ContentPage>

C#

MultiValidationBehavior 可在 C# 中按如下所示方式使用:

class MultiValidationBehaviorPage : ContentPage
{
    public MultiValidationBehaviorPage()
    {
        var entry = new Entry
        {
            IsPassword = true,
            Placeholder = "Password"
        };

        var validStyle = new Style(typeof(Entry));
        validStyle.Setters.Add(new Setter
        {
            Property = Entry.TextColorProperty,
            Value = Colors.Green
        });

        var invalidStyle = new Style(typeof(Entry));
        invalidStyle.Setters.Add(new Setter
        {
            Property = Entry.TextColorProperty,
            Value = Colors.Red
        });

        var atLeastOneDigit = new CharactersValidationBehavior
        {
            Flags = ValidationFlags.ValidateOnValueChanged,
            CharacterType = CharacterType.Digit,
            MinimumCharacterCount = 1    
        };

        MultiValidationBehavior.SetError(atLeastOneDigit, "1 digit");

        var atLeastUpperCase = new CharactersValidationBehavior
        {
            Flags = ValidationFlags.ValidateOnValueChanged,
            CharacterType = CharacterType.UppercaseLetter,
            MinimumCharacterCount = 1    
        };

        MultiValidationBehavior.SetError(atLeastUpperCase, "1 upper case");

        var atLeastOneSymbol = new CharactersValidationBehavior
        {
            Flags = ValidationFlags.ValidateOnValueChanged,
            CharacterType = CharacterType.NonAlphanumericSymbol,
            MinimumCharacterCount = 1    
        };

        MultiValidationBehavior.SetError(atLeastOneSymbol, "1 symbol");

        var atLeastEightCharacters = new CharactersValidationBehavior
        {
            Flags = ValidationFlags.ValidateOnValueChanged,
            CharacterType = CharacterType.Any,
            MinimumCharacterCount = 1    
        };

        MultiValidationBehavior.SetError(atLeastEightCharacters, "8 characters");

        var multiValidationBehavior = new MultiValidationBehavior
        {
            InvalidStyle = invalidStyle,
            ValidStyle = validStyle,
            Flags = ValidationFlags.ValidateOnValueChanged,

            Children = 
            {
                atLeastOneDigit,
                atLeastUpperCase,
                atLeastOneSymbol,
                atLeastEightCharacters
            }
        };

        entry.Behaviors.Add(multiValidationBehavior);

        Content = entry;
    }
}

C# 标记

我们的 CommunityToolkit.Maui.Markup 包提供一种更简洁的方式来在 C# 中使用此 Behavior

using CommunityToolkit.Maui.Markup;

class MultiValidationBehaviorPage : ContentPage
{
    public MultiValidationBehaviorPage()
    {
        Content = new Entry()
            .Behaviors(new MultiValidationBehavior
            {
                InvalidStyle = new Style<Entry>(Entry.TextColorProperty, Colors.Red),
                ValidStyle = new Style<Entry>(Entry.TextColorProperty, Colors.Green),
                Flags = ValidationFlags.ValidateOnValueChanged,

                Children = 
                {
                    new CharactersValidationBehavior
                    {
                        Flags = ValidationFlags.ValidateOnValueChanged,
                        CharacterType = CharacterType.Digit,
                        MinimumCharacterCount = 1    
                    }
                    .Assign(out var atLeastOneDigit),

                    new CharactersValidationBehavior
                    {
                        Flags = ValidationFlags.ValidateOnValueChanged,
                        CharacterType = CharacterType.UppercaseLetter,
                        MinimumCharacterCount = 1    
                    }
                    .Assign(out var atLeastUpperCase),

                    new CharactersValidationBehavior
                    {
                        Flags = ValidationFlags.ValidateOnValueChanged,
                        CharacterType = CharacterType.NonAlphanumericSymbol,
                        MinimumCharacterCount = 1    
                    }
                    .Assign(out var atLeastOneSymbol),

                    new CharactersValidationBehavior
                    {
                        Flags = ValidationFlags.ValidateOnValueChanged,
                        CharacterType = CharacterType.Any,
                        MinimumCharacterCount = 8  
                    }
                    .Assign(out var atLeastEightCharacters),
                }
            });

        MultiValidationBehavior.SetError(atLeastOneDigit, "1 digit");
        MultiValidationBehavior.SetError(atLeastUpperCase, "1 upper case");
        MultiValidationBehavior.SetError(atLeastOneSymbol, "1 symbol");
        MultiValidationBehavior.SetError(atLeastEightCharacters, "8 characters");
    }
}

以下屏幕截图显示 Android 上生成的 MultiValidationBehavior:Android 上的 MultiValidationBehavior 的屏幕截图

属性

MultiValidationBehavior 提供如下常用验证属性。

ValidationBehavior 属性

以下属性是在基类 public abstract class ValidationBehavior 中实现的:

properties 类型​​ 描述
Flags ValidationFlags 提供一个指定如何处理验证的枚举值。
ForceValidateCommand ICommand 允许用户提供处理强制验证的自定义 ICommand
InvalidStyle Style 验证失败时应用于元素的 Style
IsNotValid bool 指示当前值是否被视为无效。
IsRunning bool 指示验证是否正在进行(一直等待异步调用完成)。
IsValid bool 指示当前值是否被视为有效。
ValidStyle Style 验证成功时应用于元素的 Style
Value object 要验证的值。
ValuePropertyName string 允许用户重写将用作要验证的值的属性。

示例

可以在 .NET MAUI 社区工具包示例应用程序中查找此行为的示例。

API

可以在 .NET MAUI 社区工具包 GitHub 存储库查看MultiValidationBehavior 的源代码