From d9f043b45d47437fe920ed7abc7b51a90e25eb56 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 15 Jan 2016 21:06:32 +0100 Subject: [PATCH] Don't allow null logical or visual children. And remove RedirectLogicalChildren which is no longer needed, or desired. --- src/Perspex.Controls/Control.cs | 43 +++++++++----------------------- src/Perspex.SceneGraph/Visual.cs | 13 ++++++++++ 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/Perspex.Controls/Control.cs b/src/Perspex.Controls/Control.cs index a1fcce135e..bda72a8bfa 100644 --- a/src/Perspex.Controls/Control.cs +++ b/src/Perspex.Controls/Control.cs @@ -278,32 +278,14 @@ namespace Perspex.Controls if (_logicalChildren == null) { var list = new PerspexList(); - LogicalChildren = list; + list.ResetBehavior = ResetBehavior.Remove; + list.Validate = ValidateLogicalChild; + list.CollectionChanged += LogicalChildrenCollectionChanged; + _logicalChildren = list; } return _logicalChildren; } - - set - { - Contract.Requires(value != null); - - if (_logicalChildren != value) - { - if (_logicalChildren != null) - { - _logicalChildren.CollectionChanged -= LogicalChildrenCollectionChanged; - } - } - - if (value is PerspexList) - { - ((PerspexList)value).ResetBehavior = ResetBehavior.Remove; - } - - _logicalChildren = value; - _logicalChildren.CollectionChanged += LogicalChildrenCollectionChanged; - } } /// @@ -520,15 +502,6 @@ namespace Perspex.Controls DataContextChanged?.Invoke(this, EventArgs.Empty); } - /// - /// Makes the control use a different control's logical children as its own. - /// - /// The logical children to use. - protected void RedirectLogicalChildren(IPerspexList collection) - { - LogicalChildren = collection; - } - /// /// Called when the property begins and ends being notified. /// @@ -566,6 +539,14 @@ namespace Perspex.Controls return null; } + private static void ValidateLogicalChild(ILogical c) + { + if (c == null) + { + throw new ArgumentException("Cannot add null to LogicalChildren."); + } + } + private void LogicalChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) diff --git a/src/Perspex.SceneGraph/Visual.cs b/src/Perspex.SceneGraph/Visual.cs index e8fa579960..4c1c41330f 100644 --- a/src/Perspex.SceneGraph/Visual.cs +++ b/src/Perspex.SceneGraph/Visual.cs @@ -125,6 +125,7 @@ namespace Perspex var visualChildren = new PerspexList(); visualChildren.ResetBehavior = ResetBehavior.Remove; + visualChildren.Validate = ValidateLogicalChild; visualChildren.CollectionChanged += VisualChildrenChanged; VisualChildren = visualChildren; } @@ -457,6 +458,18 @@ namespace Perspex } } + /// + /// Ensures a visual child is not null. + /// + /// The visual child. + private static void ValidateLogicalChild(IVisual c) + { + if (c == null) + { + throw new ArgumentNullException("Cannot add null to VisualChildren."); + } + } + /// /// Called when the 's event /// is fired.