Skip to content

DynamicResource doesn't work when combined with AppThemeBinding #13619

Closed

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

Activity

  1. added
    t/bug
    on Mar 1, 2023
  2. added
    area-xaml
    on Mar 2, 2023
  3. added and removed
    t/bug
    on Mar 2, 2023
  4. added this to the Backlog milestone on Mar 2, 2023
  5. InquisitorJax commented on May 22, 2023

    @InquisitorJax

    @PureWeen would this be the same issue when switching out ResourceDictionaries to apply theme at runtime as described in https://learn.microsoft.com/en-us/dotnet/maui/user-interface/theming?view=net-maui-7.0 ?
    Here's a repro project that implements theming as described above:
    https://github.com/InquisitorJax/MAUI-Theme
    ... but is different degrees of broken on Android and iOS :(

  6. InquisitorJax commented on Jun 5, 2023

    @InquisitorJax

    @DaveNatalieTripArc see above repository that I've linked...
    I wasn't using AppThemeBinding - just using normal binding and switching out the resource dictionaries at runtime as per the linked article.
    What fixed it for me:
    My resource dictionary files were defined without code-behind.
    So I replaced them by using the Add | New ContentPage (xaml) - and then replaced the ContentPage base class with ResourceDictionary.
    Now that the resource dictionary files have code-behind files, it seems to work:
    image

  7. StephaneDelcroix commented on Aug 7, 2023

    @StephaneDelcroix
    Contributor

    AppThemeBinding, and Binding are setting value through SetValue, not SetDynamicResource. Currently looking if there's a way to make this work... quite a challenge...

  8. added a commit that references this issue on Aug 8, 2023
    d743109
  9. added a commit that references this issue on Aug 9, 2023
    d0b7036
  10. modified the milestones: Backlog, .NET 8 on Aug 11, 2023
  11. added a commit that references this issue on Aug 15, 2023
    5312fc9
  12. ghost locked as resolved and limited conversation to collaborators on Sep 10, 2023
  13. added
    fixed-in-8.0.0-rc.1.9171
    on Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-xamlfixed-in-8.0.0-rc.1.9171platform/androidproposal/open

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions