Skip to content

DynamicResource doesn't work when combined with AppThemeBinding #13619

@DaveNatalieTripArc

Description

@DaveNatalieTripArc

Description

I have a ResourceDictionary that contains a Style element.

    <Style TargetType="Page" ApplyToDerivedTypes="True">
        <Setter Property="Padding" Value="0"/>
        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={DynamicResource Primary}, Dark={DynamicResource Secondary}}" />
    </Style>

And I have created two different ResourceDictionies that contain Colors with identical keys.

    //Theme 1
    <Color x:Key="Primary">#512BD4</Color>
    <Color x:Key="Secondary">#DFD8F7</Color>
    <Color x:Key="Tertiary">#2B0B98</Color>

    //Theme 2
    <Color x:Key="Primary">#ff0000</Color>
    <Color x:Key="Secondary">#0000ff</Color>
    <Color x:Key="Tertiary">#00ff00</Color>

The goal is be able to dynamical swap the theme file at runtime. For now, I am just loading a single theme into my App's MergedDictionaries

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Theme1.xaml" />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>


However, when I run the app, my page background is White. It's not using any of the colors from my theme.

If I change my Style rule to not using AppThemeBinding, it works as expected. <Setter Property="BackgroundColor" Value="{DynamicResource Primary}" />

If I switch to StaticResources, it also works. <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Secondary}}" />

Is it possible to combine AppThemeBinding with DynamicResource?

I've only tested this on Android.

Steps to Reproduce

  1. Create a new Maui application
  2. Modify the default Styles that use AppThemeBinding to use some DynamicResource instead of StaticResource (I recommend modifying the Page background color as mentioned above)/
  3. Observe that the DynamicResource value isn't properly applied.

Link to public reproduction project repository

https://idonthaveone.com

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 13 (API 33)

Did you find any workaround?

I can either use StaticResource, or I can remove AppThemeBinding, but I haven't found a way for both to work at the same time.

Relevant log output

No response

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions