RequiredStringValidationBehavior

RequiredStringValidationBehavior 是一种 Behavior,允许用户确定文本输入是否等于特定文本。 例如,可以根据提供的文本输入是否有效来为 Entry 控件采用不同的样式。

重要

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

语法

以下示例演示如何将 RequiredStringValidationBehavior 添加到 Entry,并基于是否已输入 RequiredString 更改 TextColor

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>

使用 RequiredStringValidationBehavior

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

<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.RequiredStringValidationBehaviorPage">

    <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>
        <Entry.Behaviors>
            <toolkit:RequiredStringValidationBehavior 
                InvalidStyle="{StaticResource InvalidEntryStyle}"
                ValidStyle="{StaticResource ValidEntryStyle}"
                Flags="ValidateOnValueChanged"
                RequiredString="MAGIC ANSWER" />
        </Entry.Behaviors>
    </Entry>

</ContentPage>

C#

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

class RequiredStringValidationBehaviorPage : ContentPage
{
    public RequiredStringValidationBehaviorPage()
    {
        var entry = new Entry();

        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 requiredStringValidationBehavior = new RequiredStringValidationBehavior
        {
            InvalidStyle = invalidStyle,
            ValidStyle = validStyle,
            Flags = ValidationFlags.ValidateOnValueChanged,
            RequiredString = "MAGIC ANSWER"
        };

        entry.Behaviors.Add(requiredStringValidationBehavior);

        Content = entry;
    }
}

C# 标记

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

using CommunityToolkit.Maui.Markup;

class RequiredStringValidationBehaviorPage : ContentPage
{
    public RequiredStringValidationBehaviorPage()
    {
        Content = new Entry()
            .Behaviors(new RequiredStringValidationBehavior
            {
                InvalidStyle = new Style<Entry>(Entry.TextColorProperty, Colors.Red),
                ValidStyle = new Style<Entry>(Entry.TextColorProperty, Colors.Green),
                Flags = ValidationFlags.ValidateOnValueChanged,
                RequiredString = "MAGIC ANSWER"
            });
    }
}

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

属性

属性 类型​​ 描述
ExactMatch bool 确定输入的文本是必须与 RequiredString 属性的全部内容匹配,还是只需包含 RequiredString 属性值即可。
RequiredString string 被用来与用户提供的值进行比较的字符串。

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 存储库查看RequiredStringValidationBehavior 的源代码