Primary APIs:
ResourceDictionary, IResourceDictionaryResourceDictionary.MergedDictionaries, ResourceDictionary.ThemeDictionariesTryGetResource(key, theme, out value)ThemeVariant (Default, Light, Dark)ThemeVariantScopeApplication.RequestedThemeVariantAssetLoader, IAssetLoaderXmlnsDefinitionAttributeRelated XAML runtime resource APIs:
DynamicResourceExtensionResourceIncludeStyleIncludeRelated markup type-converter contracts (framework parsing layer):
CultureInfoIetfLanguageTagConverterCultureInfoIetfLanguageTagConverter.CanConvertFrom(...)CultureInfoIetfLanguageTagConverter.ConvertFrom(...)Note:
CultureInfo directly instead of calling it explicitly.Layered strategy:
Application.Resources, Application.Styles).MergedDictionaries).ResourceDictionary lookup behavior:
ThemeDictionaries) including ThemeVariant.InheritVariant chain,Example:
<ResourceDictionary>
<Color x:Key="BrandColor">#0A84FF</Color>
<ResourceDictionary.MergedDictionaries>
<ResourceInclude Source="avares://MyApp/Styles/Buttons.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
ResourceDictionary is mutable and supports efficient runtime updates.
Useful members:
Count, indexer this[object key]Keys, ValuesHasResourcesSetItems(...)ContainsKey(...)EnsureCapacity(...)GetEnumerator()Pattern:
var resources = Application.Current!.Resources;
if (resources is ResourceDictionary dict)
{
dict.EnsureCapacity(64);
dict.SetItems(new[]
{
new KeyValuePair<object, object?>("SpacingM", 12d),
new KeyValuePair<object, object?>("SpacingL", 16d)
});
if (!dict.ContainsKey("FeatureEnabled"))
dict["FeatureEnabled"] = true;
}
For expensive or late materialization, use deferred APIs:
AddDeferred(object key, Func<IServiceProvider?, object?> factory)AddDeferred(object key, IDeferredContent deferredContent)AddNotSharedDeferred(object key, IDeferredContent deferredContent)Guidance:
AddDeferred for cached-on-first-use resource values,AddNotSharedDeferred when each lookup needs a fresh instance.Theme variant APIs:
ThemeVariant.Default, ThemeVariant.Light, ThemeVariant.DarkThemeVariant.InheritVariantThemeVariant and PlatformThemeVariantoperator ThemeVariantoperator PlatformThemeVariant?RequestedThemeVariant, ActualThemeVariantUse app-wide setting:
<Application RequestedThemeVariant="Default" />
Use local override scope:
<ThemeVariantScope RequestedThemeVariant="Dark">
<ContentPresenter Content="{Binding}" />
</ThemeVariantScope>
Theme dictionaries:
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="AppBackground" Color="White" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="AppBackground" Color="#202020" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
Use dynamic resources when runtime theme/resource changes should propagate automatically.
Key APIs:
DynamicResourceExtensionDynamicResourceExtension.ResourceKeyResourceInclude.Source, StyleInclude.SourceResourceInclude.Owner, StyleInclude.OwnerResourceInclude.OwnerChanged, StyleInclude.OwnerChangedGuidance:
DynamicResource for values expected to change,Project item guidance:
AvaloniaResource.avares://AssemblyName/path URIs.C# asset access:
using Avalonia.Platform;
using var stream = AssetLoader.Open(new Uri("avares://MyApp/Assets/logo.png"));
Additional APIs:
AssetLoader.Exists(...)AssetLoader.OpenAndGetAssembly(...)AssetLoader.GetAssets(...)XmlnsDefinitionAttribute maps XML namespace to CLR namespace at assembly level.
Pattern:
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "MyApp.Controls")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "MyApp.Theming")]
Effect:
clr-namespace mappings in XAML.ThemeDictionaries.DynamicResource for theme-sensitive values.XmlnsDefinition mappings explicit and non-overlapping.ThemeDictionaries entry for active variant or missing DynamicResource usage.AddDeferred factory side effects and avoid recursive same-key lookups.Source URI and inspect ResourceInclude.Owner / StyleInclude.Owner transitions.XmlnsDefinition mapping and public type visibility.CultureInfoIetfLanguageTagConverter.