This commit is contained in:
Javier Suárez 2021-07-04 18:43:41 +02:00
Родитель b030c13828
Коммит e99871708d
1 изменённых файлов: 73 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,73 @@
# 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.