Browse Source

Prevent null Panel children

pull/10256/head
Julien Lebosquain 4 years ago
parent
commit
97aa613bba
No known key found for this signature in database GPG Key ID: 1833CAD10ACC46FD
  1. 18
      src/Avalonia.Controls/Controls.cs
  2. 8
      tests/Avalonia.Controls.UnitTests/PanelTests.cs

18
src/Avalonia.Controls/Controls.cs

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Avalonia.Collections;
@ -13,7 +14,7 @@ namespace Avalonia.Controls
/// </summary>
public Controls()
{
ResetBehavior = ResetBehavior.Remove;
Configure();
}
/// <summary>
@ -21,9 +22,22 @@ namespace Avalonia.Controls
/// </summary>
/// <param name="items">The initial items in the collection.</param>
public Controls(IEnumerable<Control> items)
: base(items)
{
Configure();
AddRange(items); // virtual member call in ctor, ok for our current implementation
}
private void Configure()
{
ResetBehavior = ResetBehavior.Remove;
Validate = item =>
{
if (item is null)
{
throw new ArgumentNullException(nameof(item),
$"A null control cannot be added to a {nameof(Controls)} collection.");
}
};
}
}
}

8
tests/Avalonia.Controls.UnitTests/PanelTests.cs

@ -1,3 +1,4 @@
using System;
using System.Linq;
using Avalonia.LogicalTree;
using Avalonia.Media;
@ -133,5 +134,12 @@ namespace Avalonia.Controls.UnitTests
renderer.Verify(x => x.AddDirty(target), Times.Once);
}
[Fact]
public void Adding_Null_Child_Should_Throw()
{
var panel = new Panel();
Assert.Throws<ArgumentNullException>(() => panel.Children.Add(null!));
}
}
}

Loading…
Cancel
Save