Search code examples
xamarin.formsbehavior.net-standardattachedbehaviors

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


Since Xamarin 2.4 (and switch to .Net Standard instead of PCL) I get the following error by using my own behaviour (XAMLC is :

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

Here is the implementation (very simple):

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);
            }
        }
    }
}

The usage in the App is also very simple:

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

This compilation works with a literal MaxLength="10" and with Bindings MaxLength="{StaticResource MyValue}" but not with a value from a static class. I need the value in XAML and in some C# code, so I would like to use the Constants class.

The value in a static class is defined as follows:

public const int MAX_PORT_LENGTH = 5;

Edit 2018-01-09

The problems seems to be in the use of inner classes. The following works:

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

But not this:

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

Solution

  • Finally I found the solution

    In the previos version (I think the static setter was not complied, but interpreted during the runtime) the syntax was with . to access the inner classes:

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

    In the newer versions of Xamarin.Forsm I have to wirte + to represent the inner classes.

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