
How can I programmatically or via editor add resources to themes?
How can I programmatically or via editor add resources to themes?
Hi David,
For clarification, do you mean that you have custom resources like brushes that you wish to associate individually with light, dark, or some other theme and have them automatically be made available appropriately when those themes are selected?
Yes exactly.
Regards / David
Theme catalogs can be registered for that purpose. Our Themes.Aero assembly has a good example where this theme catalog concept can be used. Here the ThemesAeroThemeCatalog defines four XAML resource dictionary files that are pulled for similarly-named theme names.
public class ThemesAeroThemeCatalog : SystemThemeCatalog {
private static IEnumerable<ThemedResourceDictionaryReference> dictionaryReferences;
/////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC PROCEDURES
/////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Gets the collection of <see cref="ThemedResourceDictionaryReference"/> objects for the assembly.
/// </summary>
/// <value>
/// The collection of <see cref="ThemedResourceDictionaryReference"/> objects for the assembly.
/// </value>
public override IEnumerable<ThemedResourceDictionaryReference> DictionaryReferences {
get {
if (dictionaryReferences == null) {
string baseUri = ResourceHelper.GetLocationUriStringBase(Assembly.GetExecutingAssembly()) + "Themes/Includes/";
dictionaryReferences = new ThemedResourceDictionaryReference[] {
new ThemedResourceDictionaryReference() {
LocationUri = new Uri(baseUri + "AeroNormalColor.xaml", UriKind.RelativeOrAbsolute),
Themes = new string[] { ThemeNames.AeroNormalColor },
},
new ThemedResourceDictionaryReference() {
LocationUri = new Uri(baseUri + "OfficeBlack.xaml", UriKind.RelativeOrAbsolute),
Themes = new string[] { ThemeNames.Office2010Black },
},
new ThemedResourceDictionaryReference() {
LocationUri = new Uri(baseUri + "OfficeBlue.xaml", UriKind.RelativeOrAbsolute),
Themes = new string[] { ThemeNames.Office2010Blue },
},
new ThemedResourceDictionaryReference() {
LocationUri = new Uri(baseUri + "OfficeSilver.xaml", UriKind.RelativeOrAbsolute),
Themes = new string[] { ThemeNames.Office2010Silver },
},
};
}
return dictionaryReferences;
}
}
}
Note that the Themes collection could easily be things like ThemeNames.Light or ThemeNames.Dark instead.
The XAML files here are relative to the project like "(project)/Themes/Includes/AeroNormalColor.xaml".
Then a catalog registrar is defined like this:
public class ThemesAeroThemeCatalogRegistrar : ThemeCatalogRegistrarBase<ThemesAeroThemeCatalog> { }
Finally, the registrar can be called at app startup like this:
ThemesAeroThemeCatalogRegistrar.Register();
Once that infrastructure is in place, the theme manager will watch for theme changes and will use the information in the catalog to load the appropriate XAML resource dictionaries, which can contain brushes, etc.
Awesome!
Thank you for the answer!
/ David
Please log in to a validated account to post comments.