I’ve been doing a lot of mix development between Xamarin.Forms and Native Xamarin iOS/Android. Because of this, we abstract away concepts that can be shared regardless of using Xamarin.Forms or not. One thing I personally like, and use all the time, is Xamarin.Forms Behaviors. I wanted a way to use the same pattern with my native components. I went ahead and created NativeBehaviors.
Get it
- NuGet – https://www.nuget.org/packages/NativeBehaviors/
- GitHub – https://github.com/SuavePirate/NativeBehaviors
Concepts
Like mentioned above, we wanted to follow the same style as Xamarin.Forms Behaviors
without having a dependency on Xamarin.Forms. You can create a NativeBehavior
for any native control, and then handle attaching and detaching the behavior within the lifecycle of your ViewController
, Activity
, Fragment
, etc.
This helps us keep our event handling cleaned up in memory while still being useful, easy to follow, re-usable, and flexible.
Documentation
Read below for the documentation on how to build your own NativeBehaviors
and start making your controls more flexible!
NativeBehaviors
A behaviors implementation for Xamarin Native controls meant to reflect the usefulness of Xamarin.Forms.Behaviors
Installation
This project is up on NuGet now:
Install-Package NativeBehaviors
Usage
These behaviors are meant to reflect the same use as Xamarin.Forms Behaviors, but require a little more work to manage the lifecycle of events since native views don’t have a collection of Behaviors as a property.
- Create a custom Behavior on your native platform. This example will use an iOS
UITextField
TextFieldPhoneMaskBehavior.cs
/// <summary>
/// Text field phone mask behavior.
/// Format of (###) ###-####
/// </summary>
public class TextFieldPhoneMaskBehavior : NativeBehavior<UITextField>
{
public override string BehaviorName => nameof(TextFieldPhoneMaskBehavior);
private string _previousText = string.Empty;
/// <summary>
/// Attach text changed event
/// </summary>
/// <param name="bindable">Bindable.</param>
protected override void OnAttachedTo(UITextField bindable)
{
bindable.EditingChanged += Bindable_TextChanged;
}
/// <summary>
/// Detach event
/// </summary>
/// <param name="bindable">Bindable.</param>
protected override void OnDetachingFrom(UITextField bindable)
{
bindable.EditingChanged -= Bindable_TextChanged;
}
/// <summary>
/// Apply mask
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">E.</param>
void Bindable_TextChanged(object sender, EventArgs e)
{
var textField = sender as UITextField;
// only apply change if typing forward
if (textField != null && textField.Text.Length > _previousText.Length)
{
if (textField.Text.Length == 1)
{
// we have our first number, add the ( behind it
textField.Text = $"({textField.Text}";
}
if (textField.Text.Length == 4)
{
// finish the area code
textField.Text += ") ";
}
if (textField.Text.Length == 9)
{
// add dash
textField.Text += "-";
}
}
_previousText = textField.Text;
}
}
- Attach Behavior in lifecycle
MyViewController.cs
using NativeBehaviors; // needed for extension method
...
private TextFieldPhoneMaskBehavior _phoneMaskBehavior;
protected override void ViewWillAppear(bool animated)
{
...
_phoneMaskBehavior = new TextFieldPhoneMaskBehavior();
myTextField.AttachBehavior(_phoneMaskBehavior);
...
}
...
- Detach Behavior in lifecycle
MyViewController.cs
using NativeBehaviors; // needed for extension method
...
protected override void ViewDidDisappear()
{
...
myTextField.DetachBehavior(_phoneMaskBehavior);
...
}
...
Use the OnCreate
and OnDestroy
events in your Android Activities.
If you like what you see, don’t forget to follow me on twitter @Suave_Pirate, check out my GitHub, and subscribe to my blog to learn more mobile developer tips and tricks!
Interested in sponsoring developer content? Message @Suave_Pirate on twitter for details.
One thought on “Xamarin.NuGet – Native Behaviors”