I’m working on an Android Maui app.
I have all my Xaml Entry fields named.
I programmatically create Unfocused events for each Entry and they all point to the same Unfocused function.
If the sender.id is for a certain field name then I perform some validation on it.
When I get the value of the sender.id field I get a guid.
When I inspect the sender during debug I get the field name.
public async void Unfocused(object sender, FocusEventArgs e)
{
var entry = (Entry)sender;
var fieldName = entry.Id.ToString(); // this returns a guid
}
and here’s my Xaml
<Entry Text="{Binding StorageLocation}"
x:Name="txtStorageLocation"
StyleId="StorageLocation"
Placeholder="{Binding LocationOrBinPlaceholder}"
HorizontalTextAlignment="Center"
FontSize="20"/>
I’m expecting the sender.Id to be "txtStorageLocation"
>Solution :
In wpf there is a tag property, you can implement a similar in MAUI as an attached property.
namespace Maui
{
public class AttachedProperties
{
public static readonly BindableProperty TagProperty = BindableProperty.Create("Tag", typeof(string), typeof(AttachedProperties), null);
public static string GetTag(BindableObject bindable)
=> (string)bindable.GetValue(TagProperty);
public static void SetTag(BindableObject bindable, string value)
=> bindable.SetValue(TagProperty, value);
}
}
<Entry local:AttachedProperties.Tag="txtStorageLocation"
Text="{Binding StorageLocation}"
x:Name="txtStorageLocation"
StyleId="StorageLocation"
Placeholder="{Binding LocationOrBinPlaceholder}"
HorizontalTextAlignment="Center"
FontSize="20"/>
public async void Unfocused(object sender, FocusEventArgs e)
{
var entry = (Entry)sender;
var fieldName = AttachedProperties.GetTag(entry);
}
An easier approach (but ugly) is to use ClassId. Please be aware that the intended use case of this property is UI testing.