When you have a Theme ResourceDictionary Xaml file that gets loaded in the App.Xaml via a ResourceDictionarySource reference and that Theme file in turn references sub files, subsequent themes (following the same pattern) do not refresh the UI styles despite the use of DynamicResource.
Steps to Reproduce
Create a blank Xamarin.Forms project
Add 2 theme folders
In each theme folder, add a ResourceDictionary to serve as the theme name (i.e. Theme1 and Theme2), then add a separate ResourceDictionary called Colors and add a reference to it inside the theme. Add a third ResourceDictionary called Styles and add a reference to it inside the theme. Make sure both themes have the same structure with difference color values.
In App.Xaml add Theme1 to the MergedResourceDictionaries as the default theme for when the app starts.
In MainPage.Xaml add some xaml styles to use the new styles created in the themes. Make sure they are referenced as DynamicResource.
In MainPage.Xaml.cs, override OnAppearing() and add code similar to this to swap out Theme1 for Theme2:
`
var mergedDictionaries = Xamarin.Forms.Application.Current.Resources.MergedDictionaries;
if (mergedDictionaries != null)
{
mergedDictionaries.Clear();
mergedDictionaries.Add(new Theme2());
}
`
Run the app.
Expected Behavior
You should see the colors defined in Theme2.
Actual Behavior
You still see the colors from Theme1.
Basic Information
It appears that the nested MergedDictionaries don't get refreshed when the theme is swapped out.
I have noticed that if I put all of the styles into the Theme rather than reference them as a separate file, the sample will work, but as long as I try to keep Styles.xaml as a separate file, the problem persists.
Affected Devices:
Windows 10 Desktop
Android Galaxy S8
Screenshots
Spring Theme (Default Theme) showing when it should be Autumn theme:
Autumn Theme showing if you put the styles directly inside the theme Xaml file instead of referencing them via <ResourceDictionary Source="Styles.xaml" />:
As mentioned above, a workaround is to put the style definitions directly into the them xaml file rather than splitting them out into a separate or multiple separate files. This is sub-optimal for organizational purposes, but will make the theme change code work. In the attached zip file project above just comment out the references in the theme files to styles.xaml and uncomment the style code in the same file and you will see the theme begin to work as you would expect it should.
However, in a perfect world, we should be able to include nested references to ResourceDictionaries in Themes so that we can better organize our theme definitions and still have those nested references get refreshed when the theme is dynamically changed.
Description
When you have a Theme
ResourceDictionary
Xaml file that gets loaded in theApp.Xaml
via aResourceDictionary
Source
reference and that Theme file in turn references sub files, subsequent themes (following the same pattern) do not refresh the UI styles despite the use ofDynamicResource
.Steps to Reproduce
DynamicResource
.In MainPage.Xaml.cs, override
OnAppearing()
and add code similar to this to swap out Theme1 for Theme2: `var mergedDictionaries = Xamarin.Forms.Application.Current.Resources.MergedDictionaries;
`
Expected Behavior
You should see the colors defined in Theme2.
Actual Behavior
You still see the colors from Theme1.
Basic Information
It appears that the nested MergedDictionaries don't get refreshed when the theme is swapped out.
I have noticed that if I put all of the styles into the Theme rather than reference them as a separate file, the sample will work, but as long as I try to keep Styles.xaml as a separate file, the problem persists.
Version with issue:
Last known good version: ???
IDE: VS 2019
Platform Target Frameworks: Android, UWP, haven't tested iOS
Android Support Library Version:
Nuget Packages: Microsoft.NETCore.UniversalWindowsPlatform 6.2.10 NETStandard.Library 2.0.3 Xamarin.Essentials 1.5.3.1 Xamarin.Forms 4.6.0.726+446-sha.d0dc59903-azdo.3681414
Affected Devices: Windows 10 Desktop Android Galaxy S8
Screenshots
Spring Theme (Default Theme) showing when it should be Autumn theme:
Autumn Theme showing if you put the styles directly inside the theme Xaml file instead of referencing them via
<ResourceDictionary Source="Styles.xaml" />
:Reproduction Link
Xamarin.Forms_ThemeExample.zip
Workaround
As mentioned above, a workaround is to put the style definitions directly into the them xaml file rather than splitting them out into a separate or multiple separate files. This is sub-optimal for organizational purposes, but will make the theme change code work. In the attached zip file project above just comment out the references in the theme files to styles.xaml and uncomment the style code in the same file and you will see the theme begin to work as you would expect it should.
However, in a perfect world, we should be able to include nested references to ResourceDictionaries in Themes so that we can better organize our theme definitions and still have those nested references get refreshed when the theme is dynamically changed.