I have seen two seperate ways to support theming
- DynamicResource and switching in the relevant ResourceDictionary
- AppThemeBinding
I am currently using the DynamicResource markup extension and a Light.xaml and Dark.xaml resource dictionary. This does not reflect the Platform setting and is simply a local Preferences setting. When the user selects one or the other I remove the existing and load the selected
ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
if (mergedDictionaries != null)
{
ResourceDictionary currentTheme = mergedDictionaries.FirstOrDefault(d => d.Source != null && d.Source.ToString().Contains("Themes"));
if (currentTheme != null)
{
mergedDictionaries.Remove(currentTheme);
}
if (this.IsDarkModeEnabled())
{
mergedDictionaries.Add(new Resources.Styles.Themes.Dark());
}
else
{
mergedDictionaries.Add(new Resources.Styles.Themes.Light());
}
}
This does work although I have not fully implemented it yet but I noticed yesterday that the Styles that were added by the “Upgrade Assistant” (we are migrating from XF) use AppThemeBinding
For example
<Style TargetType="ActivityIndicator">
<Setter Property="Color" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource White}}" />
</Style>
What is the approach that Microsoft is advocating?
What are others using?
3