Browse Source

Add IAddChild to StyleBase to fix IDE errors when adding Style as children to ContainerQuery (#18800)

* Add IAddChild implementation too StyleBase

* We don't actually need to Add child in AddChild

* Revert "We don't actually need to Add child in AddChild"

This reverts commit c2cd3012ca7fb0b007872e8a1bf0f54ef3cf4cea.

* Only use IAddChild as generic ones are not needed to fix the issue

* Update XamlX with fixes for IAddChild

This reverts commit 393000cf92e16696c7d0a2688a52ec654cd62c6e.
Wiesław Šoltés 5 months ago
parent
commit
a847b23f51
2 changed files with 17 additions and 2 deletions
  1. 1 1
      external/XamlX
  2. 16 1
      src/Avalonia.Base/Styling/StyleBase.cs

+ 1 - 1
external/XamlX

@@ -1 +1 @@
-Subproject commit cbfec32876a79e855bd49da034214bbf2bf35b13
+Subproject commit 83567b8a50bbf612a0b1420a3dc6d8e8ebee2399

+ 16 - 1
src/Avalonia.Base/Styling/StyleBase.cs

@@ -11,7 +11,7 @@ namespace Avalonia.Styling
     /// <summary>
     /// Base class for <see cref="Style"/> and <see cref="ControlTheme"/>.
     /// </summary>
-    public abstract class StyleBase : AvaloniaObject, IStyle, IResourceProvider
+    public abstract class StyleBase : AvaloniaObject, IStyle, IResourceProvider, IAddChild
     {
         private IResourceHost? _owner;
         private StyleChildren? _children;
@@ -72,6 +72,21 @@ namespace Avalonia.Styling
         public void Add(SetterBase setter) => Setters.Add(setter);
         public void Add(IStyle style) => Children.Add(style);
 
+        void IAddChild.AddChild(object child)
+        {
+            switch (child)
+            {
+                case SetterBase setter:
+                    Setters.Add(setter);
+                    break;
+                case IStyle style:
+                    Children.Add(style);
+                    break;
+                default:
+                    throw new InvalidOperationException($"Cannot add {child.GetType()} to a style.");
+            }
+        }
+
         public event EventHandler? OwnerChanged;
 
         public bool TryGetResource(object key, ThemeVariant? themeVariant, out object? result)