Repository navigation
Description
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
- Create a new Maui application
- Modify the default Styles that use
AppThemeBindingto use some DynamicResource instead of StaticResource (I recommend modifying the Page background color as mentioned above)/ - Observe that the DynamicResource value isn't properly applied.
Link to public reproduction project repository
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
InquisitorJax commentedon May 22, 2023
@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 :(
InquisitorJax commentedon Jun 5, 2023
@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:
StephaneDelcroix commentedon Aug 7, 2023
AppThemeBinding, and Binding are setting value through SetValue, not SetDynamicResource. Currently looking if there's a way to make this work... quite a challenge...
[X] Support DynResources as AppThemeBinding values
[X] Support DynResources as AppThemeBinding values (#16597)
[net8.0] Update dependencies from xamarin/xamarin-macios (#16749)