Home > Net >  Maui Entry Id gives a guid instead of the name
Maui Entry Id gives a guid instead of the name

Time:10-08

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"

CodePudding user response:

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.

CodePudding user response:

If you really want to use a Name or Identifier on an Entry in order to process the Unfocused event, then you could implement your own derived NamedEntry class like this:

Inherited class: NamedEntry.cs

public class NamedEntry : Entry
{
    public string Identifier { get; set; }
}

Register handler in MauiProgram.cs

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder
        .UseMauiApp<App>()
        .ConfigureMauiHandlers(h =>
         {
             h.AddHandler<NamedEntry, EntryHandler>());
         });

    return builder.Build();
}

Use in XAML

<NamedEntry Text="{Binding StorageLocation}"
            Identifier="txtStorageLocation"
            StyleId="StorageLocation"
            Placeholder="{Binding LocationOrBinPlaceholder}"
            HorizontalTextAlignment="Center"
            FontSize="20"/>

Code behind

public async void Unfocused(object sender, FocusEventArgs e)
{
    var entry = (NamedEntry)sender;
    var fieldName = entry.Identifier;
}
  • Related