4289dc6108 | ||
---|---|---|
.. | ||
.NET MAUI | ||
Xamarin.Forms | ||
README-es.md | ||
README.md |
README.md
Port Xamarin.Forms Behaviors to .NET MAUI
Behaviors let you add functionality to user interface controls without having to subclass them. Instead, the functionality is implemented in a behavior class and attached to the control as if it was part of the control itself.
In .NET MAUI the same concept works exactly the same, being able to reuse all the code.
Let's see an example.
Xamarin.Forms
using Xamarin.Forms;
namespace Behaviors
{
public class NumericValidationBehavior : 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)
{
bool isValid = double.TryParse(args.NewTextValue, out double result);
((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;
}
}
}
.NET MAUI
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
namespace Behaviors
{
public class NumericValidationBehavior : 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)
{
bool isValid = double.TryParse(args.NewTextValue, out double result);
((Entry)sender).TextColor = isValid ? Colors.Black : Colors.Red;
}
}
}
What is the difference?. The code is exactly the same except for one detail, namespaces.
Xamarin.Forms namespace changes to Microsoft.Maui.Controls namespace.
On the other hand, all the basic types such as: Color, Rectangle or Point have become available in Microsoft.Maui.Graphics. For that reason, and because using colors in this Behavior we also need this new namespace.