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( ).