.Net Maui Android Sample App is not Updating Button Test

Viewed 359

After updating VS2022 to Version 17.3.0 Preview 2, I started following this tutorial to create a .NET Maui app.

https://docs.microsoft.com/en-us/learn/dotnet-maui/build-mobile-and-desktop-apps/4-exercise-create-your-first-maui-app

The Windows app runs fine and when the method OnCounterClicked is run, the Button Text is updated.

enter image description here

private void OnCounterClicked(object sender, EventArgs e)
    {
        count++;
        if (count == 1)
            CounterBtn.Text = $"Clicked {count} time";
        else
            CounterBtn.Text = $"Clicked {count} times";
        
        SemanticScreenReader.Announce(CounterBtn.Text);
    }

However, when I run in the Android emulator, the button text does not update. I have confirmed the method is running and CounterBtn.Text is getting the correct value. I have reviewed the device log and dont see any errors. I also do not see any exceptions in the IDE.

enter image description here

Here is the call stack when the SemanticScreenReader.Announce is hit.

enter image description here

2 Answers

Its sort-of working. What you are seeing is that the button doesn't resize (On Android) when text changes. So the text gets "cut off".

Work-Around #1 - make button wider:

<Button WidthRequest="200" ...

OR

Work-Around #2 - make button taller, and tell it to wrap to multiple lines:

<Button HeightRequest="100" LineBreakMode="WordWrap" ...

OR

Work-Around #3 - start with a longer text, so it is wide enough:

<Button Text="Click me Click me" ...

OR

Extreme Work-Around #4 - Maui dynamic layout not working? Create a new page! (I didn't bother making this work correctly with AppShell; it overwrites App's main page.):

    public MainPage()
    {
        InitializeComponent();
        CounterBtn.Text = InitialCounterText;
    }

    static string InitialCounterText = "Click me";

    private void OnCounterClicked(object sender, EventArgs e)
    {
        ...

        // New page!
        InitialCounterText = CounterBtn.Text;
        Application.Current.MainPage = new MainPage();
    }

NOTE: The following should cause it to re-size, but doesn't.
This is effectively what happens when text changes, so its the same cause as the above bug.

    SemanticScreenReader.Announce(CounterBtn.Text);

    // Maui Bug! Should resize the button on Android.
    ((IView)CounterBtn).InvalidateArrange();

(I also tested InvalidateArrange on the containing VerticalStackLayout. That did not help either.)

.Net Maui Android Sample App does not changes button dimensions.

CounterBtn2.WidthRequest = a;
CounterBtn2.HeightRequest = b;
Related