Browse Source

Fix Inlines clear handling (#14247)

* Make sure to clean up VisualChildren and created text runs when the TextLayout is invalidate

* Remove localized comment

---------

Co-authored-by: Tim <47110241+timunie@users.noreply.github.com>
#Conflicts:
#	src/Avalonia.Controls/TextBlock.cs
release/11.0.8
Benedikt Stebner 2 years ago
committed by Max Katz
parent
commit
33b1bfb8d2
  1. 11
      src/Avalonia.Controls/TextBlock.cs
  2. 16
      tests/Avalonia.Controls.UnitTests/TextBlockTests.cs

11
src/Avalonia.Controls/TextBlock.cs

@ -671,9 +671,12 @@ namespace Avalonia.Controls
{
_textLayout?.Dispose();
_textLayout = null;
VisualChildren.Clear();
InvalidateVisual();
_textRuns = null;
InvalidateVisual();
InvalidateMeasure();
}
@ -690,8 +693,6 @@ namespace Avalonia.Controls
if (HasComplexContent)
{
VisualChildren.Clear();
var textRuns = new List<TextRun>();
foreach (var inline in inlines!)
@ -875,7 +876,9 @@ namespace Avalonia.Controls
}
}
private readonly struct InlinesTextSource : ITextSource
#pragma warning disable CA1815
protected readonly struct InlinesTextSource : ITextSource
#pragma warning restore CA1815
{
private readonly IReadOnlyList<TextRun> _textRuns;

16
tests/Avalonia.Controls.UnitTests/TextBlockTests.cs

@ -224,5 +224,21 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(underline, target.Inlines[0].TextDecorations);
}
}
[Fact]
public void TextBlock_TextLines_Should_Be_Empty()
{
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
{
var textblock = new TextBlock();
textblock.Inlines?.Add(new Run("123"));
textblock.Measure(new Size(200, 200));
int count = textblock.TextLayout.TextLines[0].TextRuns.Count;
textblock.Inlines?.Clear();
textblock.Measure(new Size(200, 200));
int count1 = textblock.TextLayout.TextLines[0].TextRuns.Count;
Assert.NotEqual(count, count1);
}
}
}
}

Loading…
Cancel
Save