UriValidationBehavior

UriValidationBehavior 是一种 Behavior,它允许用户确定文本输入是否为有效的 URI。 例如,可以根据提供的 URI 是有效还是无效,对 Entry 控件的样式进行区别化设置。

重要

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

语法

以下示例演示如何将 UriValidationBehavior 添加到 Entry,并根据输入的文本是否为有效的绝对 URI 更改 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>

使用 UriValidationBehavior

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

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

    <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:UriValidationBehavior 
                InvalidStyle="{StaticResource InvalidEntryStyle}"
                ValidStyle="{StaticResource ValidEntryStyle}"
                Flags="ValidateOnValueChanged"
                UriKind="Absolute" />
        </Entry.Behaviors>
    </Entry>

</ContentPage>

C#

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

class UriValidationBehaviorPage : ContentPage
{
    public UriValidationBehaviorPage()
    {
        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 uriValidationBehavior = new UriValidationBehavior
        {
            InvalidStyle = invalidStyle,
            ValidStyle = validStyle,
            Flags = ValidationFlags.ValidateOnValueChanged,
            UriKind = UriKind.Absolute
        };

        entry.Behaviors.Add(uriValidationBehavior);

        Content = entry;
    }
}

C# 标记

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

using CommunityToolkit.Maui.Markup;

class UriValidationBehaviorPage : ContentPage
{
    public UriValidationBehaviorPage()
    {
        Content = new Entry()
            .Behaviors(new UriValidationBehavior
            {
                InvalidStyle = new Style<Entry>(Entry.TextColorProperty, Colors.Red),
                ValidStyle = new Style<Entry>(Entry.TextColorProperty, Colors.Green),
                Flags = ValidationFlags.ValidateOnValueChanged,
                UriKind = UriKind.Absolute
            });
    }
}

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

属性

属性 类型​​ 描述
DecorationFlags TextDecorationFlags 提供用于设置如何处理空格的枚举值。
MaximumLength int 允许的值的最大长度。
MinimumLength int 允许的值的最小长度。
RegexOptions RegexOptions 提供用于设置正则表达式选项的枚举值。
RegexPattern string 值在被接受之前必须匹配的正则表达式模式。
UriKind UriKind 确定要接受为有效 URI 的类型。

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