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

标签 xamarin.forms behavior .net-standard attachedbehaviors

自 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/

相关文章:

c# - 线程启动<线程池> : When i get data from api

c# - 使用 C# 与 NSUrlSession 的 HTTP POST 多部分表单数据

显示简单列表的 Xamarin Forms

c# - 如何将java嵌入到C#中

ios - NSPredicate 和 NSDate 的奇怪行为

msbuild - 带有 netstandard2.0 引用的 .net 4.8 项目将不会通过 msbuild 构建

c# - 在Visual Studio中,为什么所有文件都自动成为我的C#项目的一部分?

ios - MvvmCross Xamarin.Forms(iOS): Using a TabbedPage within a MasterDetail page

Java 内存行为 : Different with Thread. sleep

c# - .net 核心包中缺少 ProtoBuf 格式化程序