Some properties not updating (eg VisualState setter not working for Button BackgroundColor) with AOT enabled
See original GitHub issueDescription
The setter is not working properly when in release mode. In debug it works fine. Only noticed it for the backgroundcolor on button. TextColor works fine for button. Didn’t test anything other. Also only tested on android, don’t now if it’s the same for other platforms.
I created a simple project to demonstrate. It’s basicly the templete that you get when createing a new project with added checkbox to enable/disable the button:
<CheckBox x:Name="checkBox" />
<Button
x:Name="CounterBtn"
Text="Click me"
HorizontalOptions="Center"
IsEnabled="{Binding Source={x:Reference checkBox}, Path=IsChecked}"/>
Before | Debug | Release |
---|---|---|
![]() |
![]() |
![]() |
Only the text color changed but the background didn’t in release mode.
Steps to Reproduce
- Download linked project
- Set to release mode
- Run on local android device
- Click the checkbox to enable/disable the button
- See results
Link to public reproduction project repository
https://github.com/Kaiffa/maui-bug-visualstate-button-release
Version with bug
7.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 12.0
Did you find any workaround?
Remapping the property from BackgroundColor to Background fixed the issue. This is easy to do in the maui program app builder:
.ConfigureMauiHandlers(_ =>
{
LabelHandler.Mapper.AppendToMapping(
nameof(View.BackgroundColor),
(handler, View) => handler.UpdateValue(nameof(IView.Background)));
ButtonHandler.Mapper.AppendToMapping(
nameof(View.BackgroundColor),
(handler, View) => handler.UpdateValue(nameof(IView.Background)));
})
Relevant log output
Visual studio 17.4.1
Android device used:
Samsung Galaxy A13, Android 12
Issue Analytics
- State:
- Created 10 months ago
- Reactions:4
- Comments:30 (13 by maintainers)
Top Results From Across the Web
Colors are not working in maui release mode - Microsoft Q&A
Color, Text Color, Background Color etc, are not working in MAUI release mode. I have used/binded these properties through ViewModel/VisualState ...
Read more >Visual state manager does not work on a button in ...
1 Answer 1 ... Actually VisualStateGroup does work . However the color only changes when you're pressing on the button , the color...
Read more >Style Control States with Visual State Manager in Xamarin ...
In this video we will learn what the Visual State Manager is, ... with VisualStateManager to Update Multiple Controls 16:28 Working Sample ...
Read more >How to Use Xamarin.Forms Visual State Manager
This blog provides details about using Visual State Manager in Xamarin.Forms to customize the property of Syncfusion button control on ...
Read more >Changelog | Cypress Documentation
Fixed an issue where a value for the Electron debug port would not be respected if defined using the ELECTRON_EXTRA_LAUNCH_ARGS environment variable. Fixes...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Can this be backported when the PR is complete to .Net 7 please
We are also facing this in iOS for all buttons in our app in release mode where they use Visual state
After a vast amount of logging in places, I found that AOT affected the order of types being referenced - and thus our static fields were being initialized in “incorrect” orders.
This happened in our “Remap for Controls” section and in most cases we were replacing fields instead of adding properties. We had the correct order in Debug because we were maybe lucky, but the AOT compiler rather went by assembly at a time instead of JIT field init.
In any case, the contents of the mappers should not be affected by the order of the references. I can actually replicate the same issue if I add this code before anything in the MauiProgram:
The issue is pretty simple now that I know what it is 😃
The mappers are set up in Core like this:
This seems like it may work, but what is actually happening in 4-6 is that the field is replaced instead of being appended. This results in step 5 (Element.ControlsElementMapper) adding fields that never make it to the Button.ControlsButtonMapper because that init code has already run and copied the old mapper from 3.
Going to look at the best way to fix this to avoid order-based-init.