gpt4 book ai didi

xamarin.forms - 找不到 'MaxLength' 的属性、可绑定(bind)属性或事件,或者值和属性之间的类型不匹配

转载 作者:行者123 更新时间:2023-12-04 17:47:51 24 4
gpt4 key购买 nike

自 Xamarin 2.4(并切换到 .Net Standard 而不是 PCL)以来,我通过使用自己的行为得到以下错误(XAMLC 是:

No property, bindable property, or event found for 'MaxLength', or mismatching type between value and property.

下面是实现(非常简单):

using Xamarin.Forms;

namespace com.rsag.xflib.Behaviors
{
/// <summary>
/// Constrain the number of charachters on entry to the given length
/// </summary>
public class MaxLengthEntryBehavior : Behavior<Entry>
{
/// <summary>
/// Value to prevent constraint
/// </summary>
public const int NOT_CONSTRAINED = 0;

/// <summary>
/// Bindable property for <see cref="MaxLength" />
/// </summary>
public static readonly BindableProperty MaxLengthProperty = BindableProperty.Create(nameof(MaxLength),
typeof(int), typeof(MaxLengthEntryBehavior), NOT_CONSTRAINED, validateValue: ValidateMaxValue);

/// <summary>
/// Max. length for the text (-1: not constrained)
/// </summary>
public int MaxLength
{
get => (int) GetValue(MaxLengthProperty);
set => SetValue(MaxLengthProperty, value);
}

private static bool ValidateMaxValue(BindableObject bindable, object value)
{
if (value is int intValue)
{
return intValue >= NOT_CONSTRAINED;
}

return false;
}

/// <inheritdoc />
protected override void OnAttachedTo(Entry bindable)
{
if (bindable != null)
{
bindable.TextChanged += OnTextChanged;
}
}

/// <inheritdoc />
protected override void OnDetachingFrom(Entry bindable)
{
if (bindable != null)
{
bindable.TextChanged -= OnTextChanged;
}
}

private void OnTextChanged(object sender, TextChangedEventArgs e)
{
if (MaxLength == NOT_CONSTRAINED)
{
return;
}

if (string.IsNullOrEmpty(e.NewTextValue))
{
return;
}

var entry = (Entry) sender;

if (e.NewTextValue.Length > MaxLength)
{
entry.Text = e.NewTextValue.Substring(0, MaxLength);
}
}
}
}

App中的用法也很简单:

<Entry Text="{Binding ServerPort.Value Keyboard="Numeric">
<Entry.Behaviors>
<libBehav:MaxLengthEntryBehavior MaxLength="{x:Static ac:Constants.MAX_PORT_LENGTH}" />
</Entry.Behaviors>
</Entry>

此编译适用于文字 MaxLength="10" 和绑定(bind) MaxLength="{StaticResource MyValue}" 但不适用于来自静态类的值。我需要 XAML 和一些 C# 代码中的值,所以我想使用 Constants 类。

静态类中的值定义如下:

public const int MAX_PORT_LENGTH = 5;

编辑 2018-01-09

问题似乎出在内部类的使用上。以下作品:

MaxLength="{x:Static ac:Constants.MAX_PORT_LENGTH}"

但不是这个:

MaxLength="{x:Static ac:Constants.ServerConstraints.MAX_PORT_LENGTH}"

最佳答案

我今天遇到了类似的问题,结果发现我的 XAML 中缺少“}”。看起来您在此行中缺少“}”:

<Entry Text="{Binding ServerPort.Value Keyboard="Numeric">
^-- here

关于xamarin.forms - 找不到 'MaxLength' 的属性、可绑定(bind)属性或事件,或者值和属性之间的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47589040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com