Skip to content

Commit

Permalink
Fixed IOE on named control reload
Browse files Browse the repository at this point in the history
`StyledElement._stylesApplied` should be reset whenever a control is repopulated

Fixes #5
  • Loading branch information
Kir-Antipov committed May 2, 2024
1 parent a17e269 commit 6977fbc
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/HotAvalonia/Helpers/AvaloniaControlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ namespace HotAvalonia.Helpers;
/// </summary>
internal static class AvaloniaControlHelper
{
/// <summary>
/// The `_stylesApplied` field of the <see cref="StyledElement"/> class.
/// </summary>
private static readonly FieldInfo? s_stylesAppliedField;

/// <summary>
/// Initializes static members of the <see cref="AvaloniaControlHelper"/> class.
/// </summary>
static AvaloniaControlHelper()
{
FieldInfo? stylesAppliedField = typeof(StyledElement).GetField("_stylesApplied", BindingFlags.NonPublic | BindingFlags.Instance);
s_stylesAppliedField = stylesAppliedField?.FieldType == typeof(bool) ? stylesAppliedField : null;
}

/// <summary>
/// Loads an Avalonia control from XAML markup and initializes it.
/// </summary>
Expand Down Expand Up @@ -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<object, object> dictionaryControl)
dictionaryControl.Clear();

Expand Down

0 comments on commit 6977fbc

Please sign in to comment.