Xamarin.forms: [Bug][iOS] AppThemeColor not updated when Theme is changed while the app is running

Created on 18 Jun 2020  路  6Comments  路  Source: xamarin/Xamarin.Forms

Description

AppThemeColor not updated when Theme is changed while the app is running

Steps to Reproduce

  1. Declare an AppThemeColor : <AppThemeColor x:Key="BackgroundColor" Light="#FFFFFF" Dark="#000000" />
  2. Reference it in a style
    <Style x:Key="MyButton" TargetType="Button"> <Setter Property="Color" Value="{DynamicResource BackgroundColor}" /> </Style>
  3. Apply the style <Button Style="{DynamicResource MyButton}"/>

PS The previous code is just a sample (I apply it to custom Xaml ContentView)

Expected Behavior

When the theme is changed from iOS system settings. While the app is running AppThemeColor should update.

Actual Behavior

The AppThemeColor doesn't update.

Basic Information

Only apply to iOS works fine on Android 10.

Tested on Xamarin.Forms (4.6.0.800) (can't update due to https://github.com/xamarin/Xamarin.Forms/issues/11099).

darkmode iOS 馃崕 unverified bug

All 6 comments

Please update to 4.7.0 and use the new AppThemeBinding syntax. https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/system-theme-changes#appthemebinding-markup-extension

I used to reference colors using < AppThemeColor x:Key="BackgroundColor" Light="#FFFFFF" Dark="#000000" />
Like: BackgroundColor="{StaticResource BackgroundColor}"

I cannot do that with this new AppThemeBinding, it crashes when app start with error "Cannot determine property to provide the value for.'"

Will I have to change everywhere in the code to the BackgroundColor="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}" ????

I used to reference colors using < AppThemeColor x:Key="BackgroundColor" Light="#FFFFFF" Dark="#000000" />
Like: BackgroundColor="{StaticResource BackgroundColor}"
I cannot do that with this new AppThemeBinding, it crashes when app start with error "Cannot determine property to provide the value for.'"

Sad, I experienced the same problem, I liked to have an indirection like AppThemeColor.

Will I have to change everywhere in the code to the BackgroundColor="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}" ????

Yes it's what I've done. To ease your theming you can create styles.

I faced a similar issue too. AppThemeBinding sounds like the alternate for OnTheme. AppThemeColor feels far more useful mainly since themes are mostly (if not always) a color-specific feature. AppThemeBinding might cause a lot of code repeat.

This may not help everyone but as a temporary solution, I copied over the original source code of AppThemeColor and it works fine.

I faced a similar issue too. AppThemeBinding sounds like the alternate for OnTheme. AppThemeColor feels far more useful mainly since themes are mostly (if not always) a color-specific feature. AppThemeBinding might cause a lot of code repeat.

This may not help everyone but as a temporary solution, I copied over the original source code of AppThemeColor and it works fine.

Where can i find the code of AppThemeColor?

Thank you

I faced a similar issue too. AppThemeBinding sounds like the alternate for OnTheme. AppThemeColor feels far more useful mainly since themes are mostly (if not always) a color-specific feature. AppThemeBinding might cause a lot of code repeat.
This may not help everyone but as a temporary solution, I copied over the original source code of AppThemeColor and it works fine.

Where can i find the code of AppThemeColor?

Thank you

I had done some digging and copied what I found to this file:
https://github.com/neville-nazerane/Socially/blob/ed9ab695d57dae97b526026d8e97906754d90409/src/Socially.MobileApps/Socially.MobileApps/Utils/AppThemeColor.cs

This doesn't look like the final version but works. Beware this seems to have a memory leak issue. If you notice the constructor, it subscribes to RequestedThemeChanged event and never unsubscribes. The constructor gets called repeatedly if you have dictionaries merging in runtime.

I am only planning to use this as a temporary fix. I am planning to look at the code at some point and rewrite it.

Was this page helpful?
0 / 5 - 0 ratings