Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Maui Entry Id gives a guid instead of the name

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading