MaxLengthReachedBehavior
MaxLengthReachedBehavior
是一种 Behavior
,允许用户在其达到 InputView
允许的最大长度时触发操作。 它可以根据用户的首选方案触发 Command
或事件。 Command
和事件都将包括 InputView
的生成文本。
此外,当达到最大长度时,可以通过 ShouldDismissKeyboardAutomatically
属性(默认为 false
)解除键盘。
重要
.NET MAUI 社区工具包行为不会设置行为的 BindingContext
,因为可以通过样式共享行为,并将其应用于多个控件。 有关详细信息,请参阅 .NET MAUI 行为
语法
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>
使用 MaxLengthReachedBehavior
MaxLengthReachedBehavior
可以在 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.MaxLengthReachedBehaviorPage"
x:Name="Page">
<Entry Placeholder="Start typing until MaxLength is reached..."
MaxLength="100"
x:Name="MaxLengthEntry">
<Entry.Behaviors>
<toolkit:MaxLengthReachedBehavior
BindingContext="{Binding Path=BindingContext, Source={x:Reference MaxLengthEntry}, x:DataType=Entry}"
Command="{Binding Source={x:Reference Page}, Path=BindingContext.MaxLengthReachedCommand, x:DataType=ContentPage}" />
</Entry.Behaviors>
</Entry>
</ContentPage>
C#
MaxLengthReachedBehavior
可在 C# 中按如下所示方式使用:
class MaxLengthReachedBehaviorPage : ContentPage
{
public MaxLengthReachedBehaviorPage()
{
var entry = new Entry
{
Placeholder = "Start typing until MaxLength is reached...",
MaxLength = 100
};
var behavior = new MaxLengthReachedBehavior();
behavior.SetBinding(
MaxLengthReachedBehavior.CommandProperty,
static (MaxLengthReachedBehaviorViewModel vm) => vm.MaxLengthReachedCommand,
source: this.BindingContext);
entry.Behaviors.Add(behavior);
Content = entry;
}
}
C# 标记
我们的 CommunityToolkit.Maui.Markup
包提供一种更简洁的方式来在 C# 中使用此 Behavior
。
using CommunityToolkit.Maui.Markup;
class MaxLengthReachedBehaviorPage : ContentPage
{
public MaxLengthReachedBehaviorPage()
{
Content = new Entry
{
Placeholder = "Start typing until MaxLength is reached...",
MaxLength = 100
}.Behaviors(
new MaxLengthReachedBehavior()
.Bind(MaxLengthReachedBehavior.CommandProperty,
getter: static (ViewModel vm) => vm.MaxLengthReachedCommand,
source: this.BindingContext));
}
}
属性
属性 | 类型 | 描述 |
---|---|---|
Command |
ICommand | 当用户达到最大长度时执行的命令。 该命令的参数将包含 InputView 的 Text 。 |
ShouldDismissKeyboardAutomatically |
bool |
指示达到最大长度时是否应自动解除键盘。 |
事件
事件 | 说明 |
---|---|
MaxLengthReached |
当用户达到最大长度时引发的事件。 事件参数将包含 InputView 的 Text 。 |
示例
可以在 .NET MAUI 社区工具包示例应用程序中查找此行为的示例。
API
可以在 .NET MAUI 社区工具包 GitHub 存储库查看MaxLengthReachedBehavior
的源代码