question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Some properties not updating (eg VisualState setter not working for Button BackgroundColor) with AOT enabled

See original GitHub issue

Description

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
Screenshot_20221125-180110 Screenshot_20221125-181250 Screenshot_20221125-180059

Only the text color changed but the background didn’t in release mode.

Steps to Reproduce

  1. Download linked project
  2. Set to release mode
  3. Run on local android device
  4. Click the checkbox to enable/disable the button
  5. 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:closed
  • Created 10 months ago
  • Reactions:4
  • Comments:30 (13 by maintainers)

github_iconTop GitHub Comments

5reactions
duindaincommented, Mar 16, 2023

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

3reactions
mattleibowcommented, Mar 10, 2023

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:

ElementHandler.ElementMapper.GetKeys();
ViewHandler.ViewMapper.GetKeys();
ButtonHandler.Mapper.GetKeys();
Button.ControlsButtonMapper.GetKeys();
Element.ControlsElementMapper.GetKeys();
VisualElement.ControlsVisualElementMapper.GetKeys();

The issue is pretty simple now that I know what it is 😃

The mappers are set up in Core like this:

  1. ElementHandler.ElementMapper adds the first level of properties
  2. ViewHandler.ViewMapper takes a reference to the ElementHandler.ElementMapper field and wraps it
  3. ButtonHandler.Mapper similarly wraps ViewHandler.ViewMapper
  4. Button.ControlsButtonMapper then comes in and this is where things go wrong: it wraps the ButtonHandler.Mapper as it is designed
  5. Element.ControlsElementMapper now comes in an wraps ElementHandler.ElementMapper and adds come more members
  6. VisualElement.ControlsVisualElementMapper then wraps and appends to Element.ControlsElementMapper

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found