Rabi Satter
Rabi Satter

Reputation: 213

Xamarin Forms Behavior is not attaching

I have written a very simple Xamarin Forms Behavior to set a max length for an Entry. However, it is not attaching. The OnAttach code does not execute. This is the first behavior I have written.

I have an OnAttachedTo and OnDetachedFrom.

public class MaxLengthBehavior : Behavior<Entry>
{
    public static readonly BindableProperty MaxLengthProperty = BindableProperty.Create("MaxLength", typeof(int), typeof(MaxLengthBehavior), 0);

    public int MaxLength
    {
        get { return (int)GetValue(MaxLengthProperty); }
        set { SetValue(MaxLengthProperty, value); }
    }

    private void bindable_TextChanged(object sender, TextChangedEventArgs e)
    {

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

    protected override void OnAttachedTo(Entry bindable)
    {
        bindable.TextChanged += bindable_TextChanged;
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        bindable.TextChanged -= bindable_TextChanged;
    }
}

I use Xaml to attach to an Entry.

<Entry x:Name="entryName" Margin="35, 20, 35, 9" 
     Placeholder="What's your name?" 
     Text="{Binding Name}">
  <Entry.Behaviors>
      <b:MaxLengthBehavior MaxLength="22" />
  </Entry.Behaviors>
</Entry>

I am sure I must be doing something wrong. However, I do not see it.

Thanks.

Upvotes: 1

Views: 760

Answers (2)

Rabi Satter
Rabi Satter

Reputation: 213

It turns out that it was a build issue with VS for Mac. I had not closed VS or the project for a couple of days. Closing and reopening VS for Mac fixed the issue. I am really starting to dislike VS for Mac. I get a lot of weirdness on solutions that are not just demo apps.

Upvotes: 0

Julius Degutis
Julius Degutis

Reputation: 276

I will post what I use and maybe it will help. This works at least for me. Main difference is that MaxLength is not BindableProperty.

 public class EntryLengthValidatorBehavior : Behavior<Entry>
{
    public int MaxLength { get; set; }

    protected override void OnAttachedTo(Entry bindable)
    {
        base.OnAttachedTo(bindable);
        bindable.TextChanged += OnEntryTextChanged;
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        base.OnDetachingFrom(bindable);
        bindable.TextChanged -= OnEntryTextChanged;
    }

    void OnEntryTextChanged(object sender, TextChangedEventArgs e)
    {
        var entry = (Entry)sender;

        // if Entry text is longer then valid length
        if (entry.Text?.Length > this.MaxLength)
        {
            string entryText = entry.Text;

            entryText = entryText.Remove(entryText.Length - 1); // remove last char

            entry.Text = entryText;
        }
    }
}

and usage

<Entry Text="{Binding VatNumber}">
  <Entry.Behaviors>
    <behaviors:EntryLengthValidatorBehavior MaxLength="14" />
  </Entry.Behaviors>
</Entry>

Upvotes: 1

Related Questions