📝 This is a MAUI version of my Xamarin NuGet

Maui.StateButton Nuget Nuget

License: MIT FOSSA Status Codacy Badge

With this control you are able to create any style of button. This is possible as it acts as a wrapper to your XAML and provides you the events/ commands and properties to bind too.

It's also 100% accessible with each platform seeing/ treating the control as a native button.

The example page, has a bunch of different design to showcase the usage -

Example gif

How to use it?

Install the NuGet package into your shared project project

Install-Package IeuanWalker.StateButton

Then in the MauiProgram.cs, and the StateButton configuration method -

using StateButton;
builder
	.UseMauiApp<App>()
	.UseMauiCommunityToolkit()
	.ConfigureStateButton();

What can I do with it?

Properties

Property What it does Extra info
State This changes based on the button state. i.e. Pressed, NotPressed Default state is NotPressed

The binding mode is OneWayToSource so it can only be controlled via this control.

Events

Event What it does
Clicked Triggerd when the button is pressed and released
Pressed Triggerd when the button is pressed
Released Triggerd when the button is released

Commands

Command What it does
ClickedCommand Triggerd when the button is pressed and released
PressedCommand Triggerd when the button is pressed
ReleasedCommand Triggerd when the button is released

How to style the button for different states

Simply add a Trigger to any element and bind it to the State property of the button -

<stateButton:StateButton Padding="20,10" BackgroundColor="Red">
    <stateButton:StateButton.StrokeShape>
        <RoundRectangle CornerRadius="10" />
    </stateButton:StateButton.StrokeShape>
    <stateButton:StateButton.Triggers>
        <Trigger TargetType="stateButton:StateButton" Property="State" Value="Pressed">
            <Setter Property="BackgroundColor" Value="Green" />
        </Trigger>
    </stateButton:StateButton.Triggers>

    <Label Text="Test" TextColor="White">
        <Label.Triggers>
            <DataTrigger Binding="{Binding Source={RelativeSource AncestorType={x:Type stateButton:StateButton}}, Path=State}"
                        TargetType="Label"
                        Value="Pressed">
                <Setter Property="TextColor" Value="Blue" />
            </DataTrigger>
        </Label.Triggers>
    </Label>
</stateButton:StateButton>

Accessibility

The control itself is seen as a native button on both platforms, so inherits the same accessbility attributes. So from a screenreader and keyboard POV it is a native button.

There are a few things you can do to improve the accessibility -

  1. Set the SemanticProperties.Description property on the button. Android is smart enough, that if you dont set this property it will try and find a label within the control to read the text from, iOS does not do this, so if you dont set this property, iOS screenreader will just announce that there is a button, but no name or context is given.

  2. Optionally you can also set the SemanticProperties.Hint property. Using this property you can give more context to what the button is for/ will do.

License

FOSSA Status