Following the documentation here, I created a handler to set the horizontal content alignment on a button
This is the shared platform partial class:
public partial class IconButtonHandler
{
public static IPropertyMapper<IconButton, IconButtonHandler> PropertyMapper = new PropertyMapper<IconButton, IconButtonHandler>(ViewHandler.ViewMapper)
{
[nameof(IconButton.HorizontalContentAlignment)] = IconButtonHandler.MapHorizontalAlignment
};
public IconButtonHandler() : base(PropertyMapper)
{
}
}
This is the android implementation
public partial class IconButtonHandler : ViewHandler<IconButton, AppCompatButton>
{
public IconButtonHandler([NotNull] IPropertyMapper mapper, CommandMapper commandMapper = null) : base(mapper, commandMapper)
{
}
protected override AppCompatButton CreatePlatformView()
{
var button = new AppCompatButton(Context);
return button;
}
protected override void ConnectHandler(AppCompatButton platformView)
{
base.ConnectHandler(platformView);
}
protected override void DisconnectHandler(AppCompatButton platformView)
{
platformView.Dispose();
base.DisconnectHandler(platformView);
}
public static void MapHorizontalAlignment(IconButtonHandler handler, IconButton view)
{
handler.PlatformView.Gravity = view.HorizontalContentAlignment.ToDroidHorizontalGravity();
}
}
This is adding the handler in MauiProgram.cs
:
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(IconButton), typeof(IconButtonHandler));
})
This is IconButton
, it just inherits from button and adds this:
public TextAlignment HorizontalContentAlignment
{
get => (TextAlignment)GetValue(HorizontalContentAlignmentProperty);
set => SetValue(HorizontalContentAlignmentProperty, value);
}
public static readonly BindableProperty HorizontalContentAlignmentProperty = BindableProperty.Create(
nameof(IconButton), // the name of the bindable property
typeof(TextAlignment), // the bindable property type
typeof(IconButton), // the parent object type
TextAlignment.Center); // the default value for the property
The buttons are invisible on Android. When I comment out adding the handler, they show up again. I feel like the problem is in CreatePlatformView
, because If I create a blank handler with no logic except for this
protected override AppCompatButton CreatePlatformView()
{
var button = new AppCompatButton(Context);
return button;
}
The buttons still don't show up.