From 6977fbc533fd16a67fbd0a013318fce145ab1b02 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Thu, 2 May 2024 19:10:17 +0300 Subject: [PATCH] Fixed IOE on named control reload `StyledElement._stylesApplied` should be reset whenever a control is repopulated Fixes #5 --- .../Helpers/AvaloniaControlHelper.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs b/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs index 22d2313..e3c6a7a 100644 --- a/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs +++ b/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs @@ -11,6 +11,20 @@ namespace HotAvalonia.Helpers; /// internal static class AvaloniaControlHelper { + /// + /// The `_stylesApplied` field of the class. + /// + private static readonly FieldInfo? s_stylesAppliedField; + + /// + /// Initializes static members of the class. + /// + static AvaloniaControlHelper() + { + FieldInfo? stylesAppliedField = typeof(StyledElement).GetField("_stylesApplied", BindingFlags.NonPublic | BindingFlags.Instance); + s_stylesAppliedField = stylesAppliedField?.FieldType == typeof(bool) ? stylesAppliedField : null; + } + /// /// Loads an Avalonia control from XAML markup and initializes it. /// @@ -155,6 +169,9 @@ public static void Clear(object? control) if (control is null) return; + if (control is StyledElement) + s_stylesAppliedField?.SetValue(control, false); + if (control is IDictionary dictionaryControl) dictionaryControl.Clear();