Behaviours
Behaviours enable you to implement objects which can be concisely attached to events and behaviours of any control type. This means we can package and reuse behaviours between similar controls without having to write repetitive code behind our XAML sheets.
Let's create a new folder called Behaviours
, add in a new file called LowercaseEntryBehaviour.cs
, and implement the following:
public class LowercaseEntryBehaviour : Behavior<Entry> { protected override void OnAttachedTo(Entry entry) { entry.TextChanged += OnEntryTextChanged; base.OnAttachedTo(entry); } protected override void OnDetachingFrom(Entry entry) { entry.TextChanged -= OnEntryTextChanged; base.OnDetachingFrom(entry); } void OnEntryTextChanged(object sender, TextChangedEventArgs args) { ((Entry)sender).Text = args.NewTextValue.ToLower(); } }
The OnAttachedTo
and OnDetachingFrom
methods get invoked when the behavior...