Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend MauiMaterialButton to expose a constructor which accepts a style id override #22455

Open
EquinoX28 opened this issue May 16, 2024 · 0 comments
Labels

Comments

@EquinoX28
Copy link

Description

I would like to have the possibility to create a MauiMaterialButton with a custom android style.
The current implementation extends MaterialButton and is exposing a constructor that accepts the android context only.

public MauiMaterialButton(Context context) : base(context) { }

Materialbutton also has a constructor accepting a style override (defStyleAttr).

public unsafe MaterialButton(Context context, IAttributeSet? attrs, int defStyleAttr) : base(IntPtr.Zero, JniHandleOwnership.DoNotTransfer) {...}

Without exposing this constructor it seems like there's no simple way to extend MauiMaterialButton to benefit from automatic image icon scaling and still define a custom (or variable) android style.

Public API Changes

public MauiMaterialButton(Context context, IAttributeSet? attrs, int defStyleAttr) : base(Context context, IAttributeSet? attrs, int defStyleAttr) { }

Intended Use-Case

I have a situation where I select an android style in a derived button handler based on a buttonType enum property.

private static int GetStyle(BindableObject element)
{
    int style = Resource.Attribute.materialButtonStyle;

    if (element is CustomButton btn)
    {
        var btnType = btn.ButtonType;
        switch (btnType)
        {
            case CustomButtonType.Contained:
                break;

            case CustomButtonType.Outlined:
                style = Resource.Attribute.materialOutlinedButtonStyle;
                break;

           ...

            case CustomButtonType.Text:
                style = Resource.Attribute.materialTextButtonStyle;
                break;
        }
    }
    return style;
}

For this I need to override CreatePlatformView and create a new Materialbutton with the android style that corresponds to the buttonType enum. I can't use the MauiMaterialButton for this yet, as it does not expose the necessary constructor.

var button = new MaterialButton(Context, null, GetStyle((BindableObject)VirtualView))

The android styles are based on the Widget.MaterialComponents.Button styles, e.g. TextButton and OutlinedButton.

@PureWeen PureWeen added this to the .NET 9 Planning milestone May 17, 2024
@Eilon Eilon added the area-controls-button Button, ImageButton label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants