Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Kaiffa opened this issue Nov 25, 2022 · 32 comments · Fixed by #13836
Assignees
Labels
area-controls-button Button, ImageButton area-xaml XAML, CSS, Triggers, Behaviors delighter fixed-in-8.0.0-preview.7.8842 Look for this fix in 8.0.0-preview.7.8842! p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint partner/cat 😻 this is an issue that impacts one of our partners or a customer our advisory team is engaged with platform/android 🤖 platform/iOS 🍎 t/bug Something isn't working
Milestone

Comments

@Kaiffa
Copy link

Kaiffa commented Nov 25, 2022

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
@Kaiffa Kaiffa added the t/bug Something isn't working label Nov 25, 2022
@ghost ghost added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Nov 25, 2022
@jsuarezruiz jsuarezruiz added this to the Backlog milestone Nov 28, 2022
@ghost
Copy link

ghost commented Nov 28, 2022

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@mattleibow
Copy link
Member

mattleibow commented Dec 8, 2022

I tried a few things, but found something that makes me feel this is an Android AOT issue... somehow.

If I force XamlC in debug, no issue:

<_MauiForceXamlCForDebug>True</_MauiForceXamlCForDebug>

If I disable the AOT but keep trimming on, no issue:

<PublishTrimmed>true</PublishTrimmed>
<RunAOTCompilation>false</RunAOTCompilation>

However, If I now enable AOT, the issue returns:

<PublishTrimmed>true</PublishTrimmed>
<RunAOTCompilation>true</RunAOTCompilation>

@jonathanpeppers @rolfbjarne how would one go about seeing where/why this happens?

@mattleibow
Copy link
Member

There is a workaround in #8961 that seems to work as well:

Use the Background property instead of BackgroundColor and the button color changes as expected. Just need to make sure to use Brush instead of Color as the values.

@mattleibow
Copy link
Member

Comments in #8961 also seem to indicate this happens for iOS on TestFlight - probably mono AOT at this point...

@mattleibow mattleibow added platform/iOS 🍎 area-xaml XAML, CSS, Triggers, Behaviors partner/macios Issues for the Mac / iOS SDK partner/android Issues for the Android SDK labels Dec 8, 2022
@mattleibow mattleibow changed the title [Android] VisualState setter not working for Button BackgroundColor in release mode VisualState setter not working for Button BackgroundColor with AOT enabled Dec 8, 2022
@samhouts samhouts added the p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint label Dec 17, 2022
@BioTurboNick
Copy link
Contributor

Related to #9753 ?

@steveisok
Copy link
Member

/cc @fanyang-mono

@hartez
Copy link
Contributor

hartez commented Feb 1, 2023

So @steveisok and @fanyang-mono - are there any working theories on why this is happening? Is there anything we can be doing on our side to move this forward?

@mikeparker104 mikeparker104 added the partner/cat 😻 this is an issue that impacts one of our partners or a customer our advisory team is engaged with label Feb 2, 2023
@steveisok
Copy link
Member

@hartez Nothing off the top of my head. I expect it's going to take a bit of sleuthing. @fanyang-mono when do you expect to be able to work on this?

@fanyang-mono
Copy link
Member

I could start working on this next week. First, I will be working on creating an app hopefully a desktop app, which could reproduce this issue. That would be easier for me to debug Mono with.

@samhouts samhouts added partner/mono external and removed partner/macios Issues for the Mac / iOS SDK partner/android Issues for the Android SDK labels Feb 6, 2023
@satya-prismhr
Copy link

I have migrated the Xamarin forms app to .NET MAUI.
Is there any plan to fix this in .NET7. Using Background instead of BackgroundColor is not the solution for me where BackgroundColor changing from ViewModel/VisualState/Trigger at so many places so applying this every where doesn't seems to be solution to me.

Is there any Handler/Renderer way so that I can write one place and fixes everywhere. @mattleibow @jonathanpeppers @fanyang-mono

@mattleibow
Copy link
Member

I am afk now, so apologies for the short reply...

You can try adding a new mapper in mauiprogram.cs

ViewHandler.ViewMapper.AppendToMapping(nameof(VisualElement.BackgroundColor), (v, h) => h.UpdateValue(nameof(IView.Backgeound)));

@satya-prismhr
Copy link

I am afk now, so apologies for the short reply...

You can try adding a new mapper in mauiprogram.cs

ViewHandler.ViewMapper.AppendToMapping(nameof(VisualElement.BackgroundColor), (v, h) => h.UpdateValue(nameof(IView.Backgeound)));

Hi @mattleibow,
Thanks for the reply.
Added the code you suggested in mauiprogram.cs, It didn't fixed the Issue.
image

@curia-damiano
Copy link

Interested as well in eventual workarounds for .NET 7.
My customer don't have access to the source codes, as this is in NuGet packages that they are importing.

@Fax
Copy link

Fax commented Jun 7, 2023

I am here just to tell you that the BackgroundColor doesn't change even when in debug, tested on a fresh project and building it for windows. How come that nobody tested the "isEnabled" flag on a button?

@phispi
Copy link

phispi commented Jul 12, 2023

As denhaandrei mentioned, the only change I needed to do in my MAUI application to avoid this problem is in the file Resources/Styles/Styles.xaml in the section <Style TargetType="Button"> it to replace BackgroundColor with Background:

--- a/mauiproject/Resources/Styles/Styles.xaml
+++ b/mauiproject/Resources/Styles/Styles.xaml
@@ -25,7 +25,7 @@

     <Style TargetType="Button">
         <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource Primary}}" />
-        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource White}}" />
+        <Setter Property="Background" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource White}}" />
         <Setter Property="FontFamily" Value="OpenSansRegular"/>
         <Setter Property="FontSize" Value="14"/>
         <Setter Property="CornerRadius" Value="8"/>
@@ -39,7 +39,7 @@
                     <VisualState x:Name="Disabled">
                         <VisualState.Setters>
                             <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray950}, Dark={StaticResource Gray200}}" />
-                            <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray600}}" />
+                            <Setter Property="Background" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray600}}" />
                         </VisualState.Setters>
                     </VisualState>
                 </VisualStateGroup>

@samhouts samhouts modified the milestones: .NET 8, .NET 8 GA Jul 12, 2023
@SarthakB26
Copy link

As denhaandrei mentioned, the only change I needed to do in my MAUI application to avoid this problem is in the file Resources/Styles/Styles.xaml in the section <Style TargetType="Button"> it to replace BackgroundColor with Background:

--- a/mauiproject/Resources/Styles/Styles.xaml
+++ b/mauiproject/Resources/Styles/Styles.xaml
@@ -25,7 +25,7 @@

     <Style TargetType="Button">
         <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource Primary}}" />
-        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource White}}" />
+        <Setter Property="Background" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource White}}" />
         <Setter Property="FontFamily" Value="OpenSansRegular"/>
         <Setter Property="FontSize" Value="14"/>
         <Setter Property="CornerRadius" Value="8"/>
@@ -39,7 +39,7 @@
                     <VisualState x:Name="Disabled">
                         <VisualState.Setters>
                             <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray950}, Dark={StaticResource Gray200}}" />
-                            <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray600}}" />
+                            <Setter Property="Background" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray600}}" />
                         </VisualState.Setters>
                     </VisualState>
                 </VisualStateGroup>

Yes we know of this solution but this would override all the BackgroundColor property set anywhere in the app. Which can be issue when migrating Xamarin apps where people were not using Background property at all. In that case you would have to replace BackgroundColor with Background through out the app which is not cool at all.

@bofden
Copy link

bofden commented Jul 13, 2023

As denhaandrei mentioned, the only change I needed to do in my MAUI application to avoid this problem is in the file Resources/Styles/Styles.xaml in the section <Style TargetType="Button"> it to replace BackgroundColor with Background:

--- a/mauiproject/Resources/Styles/Styles.xaml
+++ b/mauiproject/Resources/Styles/Styles.xaml
@@ -25,7 +25,7 @@

     <Style TargetType="Button">
         <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource Primary}}" />
-        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource White}}" />
+        <Setter Property="Background" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource White}}" />
         <Setter Property="FontFamily" Value="OpenSansRegular"/>
         <Setter Property="FontSize" Value="14"/>
         <Setter Property="CornerRadius" Value="8"/>
@@ -39,7 +39,7 @@
                     <VisualState x:Name="Disabled">
                         <VisualState.Setters>
                             <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray950}, Dark={StaticResource Gray200}}" />
-                            <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray600}}" />
+                            <Setter Property="Background" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray600}}" />
                         </VisualState.Setters>
                     </VisualState>
                 </VisualStateGroup>

Yes we know of this solution but this would override all the BackgroundColor property set anywhere in the app. Which can be issue when migrating Xamarin apps where people were not using Background property at all. In that case you would have to replace BackgroundColor with Background through out the app which is not cool at all.

I had to solve it for my customer too, so I disabled AOT compilation in project by adding this line into my CSPROJ file in the first "PropertyGroup".
<RunAOTCompilation>false</RunAOTCompilation>

This was the fastest way to solve it, since the issue is opened for more then half year...

@phispi
Copy link

phispi commented Jul 13, 2023

Yes we know of this solution but this would override all the BackgroundColor property set anywhere in the app. Which can be issue when migrating Xamarin apps where people were not using Background property at all. In that case you would have to replace BackgroundColor with Background through out the app which is not cool at all.

Thanks for the clarification. But does anything speak against using this workaround for apps that are freshly created with Maui?

@SarthakB26
Copy link

Yes we know of this solution but this would override all the BackgroundColor property set anywhere in the app. Which can be issue when migrating Xamarin apps where people were not using Background property at all. In that case you would have to replace BackgroundColor with Background through out the app which is not cool at all.

Thanks for the clarification. But does anything speak against using this workaround for apps that are freshly created with Maui?

The only thing you need to make sure if you are using this workaround is that from now wards you need to use Backgroud property to change/set backgroundcolor property of Button. Setting BackgroundColor will have no effect and will be override by the Background value set in ButtonStyle in App.xaml if not explicitly set on particular Button.

Yup For new apps this works good enough

@mattleibow
Copy link
Member

I found a workaround for now that is quite simple:

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)));
})

@StephenWin
Copy link

StephenWin commented Aug 13, 2023

Will this be fixed soon, it is very fundamental for us to be able to release that the buttons show no colour on pressing. Testing in debug is of course all ok. I am beginning to despair that it is impossible to release a large Maui project to customers.

@pulmuone
Copy link

When will this bug be fixed?
Migrating Xamarin.Forms to Maui (.net7) and having issues with Android (aot) apk.

@mattleibow
Copy link
Member

This bug is fixed but will not be backported to .NET 7 as the changes required are too extensive. There is a workaround and .NET 8 RC 1 (with a go live license too I think) has the fix.

@ghost ghost locked as resolved and limited conversation to collaborators Oct 12, 2023
@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-button Button, ImageButton area-xaml XAML, CSS, Triggers, Behaviors delighter fixed-in-8.0.0-preview.7.8842 Look for this fix in 8.0.0-preview.7.8842! p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint partner/cat 😻 this is an issue that impacts one of our partners or a customer our advisory team is engaged with platform/android 🤖 platform/iOS 🍎 t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.