diff --git a/src/Avalonia.Base/Controls/ResourceNodeExtensions.cs b/src/Avalonia.Base/Controls/ResourceNodeExtensions.cs index 382ebac0e3..c307b709fe 100644 --- a/src/Avalonia.Base/Controls/ResourceNodeExtensions.cs +++ b/src/Avalonia.Base/Controls/ResourceNodeExtensions.cs @@ -221,11 +221,25 @@ namespace Avalonia.Controls { _owner.ResourcesChanged += ResourcesChanged; } + if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) + { + themeVariantHost.ActualThemeVariantChanged += ActualThemeVariantChanged; + } } protected override void Deinitialize() { _target.OwnerChanged -= OwnerChanged; + + if (_owner is not null) + { + _owner.ResourcesChanged -= ResourcesChanged; + } + if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) + { + themeVariantHost.ActualThemeVariantChanged -= ActualThemeVariantChanged; + } + _owner = null; } @@ -253,7 +267,7 @@ namespace Avalonia.Controls } if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) { - themeVariantHost.ActualThemeVariantChanged += ActualThemeVariantChanged; + themeVariantHost.ActualThemeVariantChanged -= ActualThemeVariantChanged; } _owner = _target.Owner; @@ -264,7 +278,7 @@ namespace Avalonia.Controls } if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost2) { - themeVariantHost2.ActualThemeVariantChanged -= ActualThemeVariantChanged; + themeVariantHost2.ActualThemeVariantChanged += ActualThemeVariantChanged; } PublishNext(); diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ThemeDictionariesTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ThemeDictionariesTests.cs index 2def84bb18..1160e8d9c5 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ThemeDictionariesTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ThemeDictionariesTests.cs @@ -110,6 +110,39 @@ public class ThemeDictionariesTests : XamlTestBase Assert.Equal(Colors.Black, ((ISolidColorBrush)border.Background)!.Color); } + [Fact] + public void DynamicResource_In_ResourceProvider_Updated_When_Control_Theme_Changed() + { + var themeVariantScope = new ThemeVariantScope + { + RequestedThemeVariant = ThemeVariant.Light, + Resources = new ResourceDictionary + { + ThemeDictionaries = + { + [ThemeVariant.Dark] = new ResourceDictionary { ["DemoBackground"] = Brushes.Black }, + [ThemeVariant.Light] = new ResourceDictionary { ["DemoBackground"] = Brushes.White } + } + }, + Child = new Border() + }; + + var resources = (ResourceDictionary)AvaloniaRuntimeXamlLoader.Load(@" + + +"); + + themeVariantScope.Resources.MergedDictionaries.Add(resources); + var geo = (GeometryDrawing)themeVariantScope.FindResource("Geo"); + + Assert.NotNull(geo); + Assert.Equal(Colors.White, ((ISolidColorBrush)geo.Brush)!.Color); + + themeVariantScope.RequestedThemeVariant = ThemeVariant.Dark; + + Assert.Equal(Colors.Black, ((ISolidColorBrush)geo.Brush)!.Color); + } + [Fact] public void Intermediate_StaticResource_Can_Be_Reached_From_ThemeDictionaries() {