Anomaly Changing colors in .NET MAUI Android Release publication

Stephen Hustedde 21 Reputation points
2022-10-23T20:31:36.337+00:00

I was creating a game in .NET MAUI (Visual Studio 2022 version 17.3.5) in which I call a method to cycle through a collection of buttons to change colors based on the Text of the button. Works great in Debug mode. But once published as an APK file, the method does not function correctly. I’ve replicated the problem with a simple project:

----------

MainPage.xaml:

    <ScrollView>  
        <VerticalStackLayout  
          <Label x:Name ="lblOutput"   
              	Text="Test of MAUI differnece in Debug v. Release"  
              	FontSize="18” HorizontalOptions="Center" />  
    <HorizontalStackLayout HorizontalOptions="Center">  
    <Button x:Name="btn1" FontSize="Medium" Margin="10" Text="A"   
                  Clicked="ProcessInput" FontAttributes="Bold" BackgroundColor="Black"/>  
    <Button x:Name="btn2" FontSize="Medium" Margin="10" Text="B"   
                  Clicked="ProcessInput" FontAttributes="Bold" BackgroundColor="Black"/>			<Button x:Name="btn3" FontSize="Medium" Margin="10" Text="C"   
                  Clicked="ProcessInput" FontAttributes="Bold" BackgroundColor="Black"/>  
                 <Button x:Name="btn4" FontSize="Medium" Margin="10" Text="D"   
                  Clicked="ProcessInput" FontAttributes="Bold" BackgroundColor="Black"/>  
                 <Button x:Name="btn5" FontSize="Medium" Margin="10" Text="E"   
                  Clicked="ProcessInput" FontAttributes="Bold" BackgroundColor="Black"/>  
    </HorizontalStackLayout>  
       </VerticalStackLayout>  
    </ScrollView>\  

MainPage.xaml.cs:

	private void ProcessInput(object sender, EventArgs e)  
	{  
		Button temp = sender as Button;  
		char choice = temp.Text[0];  
        lblOutput.Text = "You chose " + choice.ToString();  
        char correctChoice= 'D';  
        if (choice == correctChoice)  
        {  
            HighlightAnswer(choice, Colors.Green);  
        } else {  
            HighlightAnswer(choice, Colors.Red);  
        }  
	}  
  
    private void HighlightAnswer(char letter, Color c)  
    {  
        Button[ ] myButtons = { btn1, btn2, btn3, btn4, btn5 };  
        foreach (Button b in myButtons)  
        {  
            if (Char.Parse(b.Text) == letter)  
            {  
                b.BackgroundColor = c;  // RELEASE ERROR - this works in Debug config but not in Release config  
                ShowDebugMessage("Color should have been changed for Button " + b.Text);  
            }  
        }  
    }  
  
    async void ShowDebugMessage(string prompt)  
    {  
        await App.Current.MainPage.DisplayAlert("DEBUG MESSAGE", prompt, "Okay");  
    }  

----------

The ShowDebugMessage call reveals that the conditional code is executing, but the color is not changing in the Release. I tried hard coding the color to “Colors.Blue instead of c. No change.

Is this just a .NET MAUI bug? Is there a suggested work around for changing colors of a view programmatically in a condition? I also tried FromARGB( ).

253318-maui-debug-v-release-color-change.jpg

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,137 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,528 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,771 Reputation points Microsoft Vendor
    2022-10-24T06:25:41.703+00:00

    Hello,

    Is this just a .NET MAUI bug? Is there a suggested work around for changing colors of a view programmatically in a condition? I also tried FromARGB( ).

    Yes, I find a similar issue in the github:Button BackgroundColor does not change with IsEnabled State on Release to Device Only.

    Here is a workaround: Use the Background property instead of BackgroundColor. Such as b.Background = c; in your code.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.