dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

[7.096] Setting .NET MAUI Button.Text to String.Empty inside a Clicked event handler causes previously set buttons to revert to previous values #18235

Closed andrewstellman closed 3 weeks ago

andrewstellman commented 1 year ago

Description

There seems to be a .NET MAUI bug in the Button control where it displays text after its Text property has been set to an empty string. When a Button control's text is cleared by setting its Text property equal to "" or String.Empty, buttons that were previously also cleared the same way will display their previous values.

This only seems to happen on maccatalyst. I was unable to reproduce it on Windows or Android.

Steps to Reproduce

Add multple Button controls to a .NET MAUI page. For example:

            <Button Clicked="Button_Clicked" x:Name="Button1" Text="A" />
            <Button Clicked="Button_Clicked" x:Name="Button2" Text="B" />
            <Button Clicked="Button_Clicked" x:Name="Button3" Text="C" />
            <Button Clicked="Button_Clicked" x:Name="Button4" Text="D" />

Add a Clicked event handler that sets its text to an empty string. For example:

    private void Button_Clicked(object sender, EventArgs e)
    {
        if (sender is Button buttonClicked)
            buttonClicked.Text = String.Empty;
    }

Run the app:

dotnet build -t:Run -f net7.0-maccatalyst
  1. Click the A button. The A disappears from the button.
  2. click the B button. The B disappears from the button.
    • Expected: The 'A' button stays blank.
    • Actual: The 'A' reappears on the A button.
  3. click the C button. The C disappears from the button.
    • Expected: The 'B' button stays blank.
    • Actual: The 'B' reappears on the B button.
  4. click the D button. The D disappears from the button.
    • Expected: The 'C' button stays blank.
    • Actual: The 'C' reappears on the A button.

Here's a screenshot from my sample app in the repo below:

image

The first three buttons should be blank. The Label underneath the buttons shows the Text property values.

Link to public reproduction project repository

https://github.com/andrewstellman/reproduce-maui-button-text-bug

Version with bug

7.0.96

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

Unknown/Other

Affected platforms

macOS

Affected platform versions

No response

Did you find any workaround?

The only workaround I could find was to use a space instead of String.Empty when clearing the button label.

Relevant log output

No response

ghost commented 1 year ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

XamlTest commented 11 months ago

Verified this on Visual Studio Enterprise 17.9.0 Preview 1(8.0.3). Repro on iOS 17.0 and MacCatalyst, not repro on Windows 11 and Android 14.0-API34 with below Project: reproduce-maui-button-text-bug.zip

grantlogsdon1 commented 6 months ago

Im seeing the same issue in 8.0.7 with iOS 17.2. Are there any updates or a workaround for this?

MichaelLHerman commented 3 months ago

Experiencing this on iOS as well. I have a button with an image and want to programmatically make the text disappear or reappear.

I hate it, but using unicode zero-width space appears to work as a workaround.

CounterBtn.Text = "\u200B";
CounterBtn.ContentLayout =
                    new Button.ButtonContentLayout(Button.ButtonContentLayout.ImagePosition.Left, 0);
NeilMalcolm commented 2 months ago

This is reproduceable in 8.0.82 on iOS as well. This is when changing text via any means, so not just an event handler but also Bindings.

I have a reproduction here https://github.com/NeilMalcolm/MauiButtonBindingIssue