From 886b83729177d368d7f1cc38dd1796e1712af5b1 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 5 Mar 2016 15:29:08 +0100 Subject: [PATCH] Allow OnDetachedFromLogicalTree to be fired > once. As it can happen now that TemplatedControl is correctly detaching template children. --- src/Perspex.Controls/Control.cs | 38 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Perspex.Controls/Control.cs b/src/Perspex.Controls/Control.cs index 8ab69f671a..6aa7359783 100644 --- a/src/Perspex.Controls/Control.cs +++ b/src/Perspex.Controls/Control.cs @@ -515,29 +515,29 @@ namespace Perspex.Controls /// protected virtual void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) { - if (!_isAttachedToLogicalTree) - { - throw new Exception("Logic error: Control is not attached to logical tree"); - } - - if (Name != null) + if (_isAttachedToLogicalTree) { - _nameScope?.Unregister(Name); - } + if (Name != null) + { + _nameScope?.Unregister(Name); + } - _isAttachedToLogicalTree = false; - _styleDetach.OnNext(Unit.Default); - this.TemplatedParent = null; - DetachedFromLogicalTree?.Invoke(this, e); + _isAttachedToLogicalTree = false; + _styleDetach.OnNext(Unit.Default); + this.TemplatedParent = null; + DetachedFromLogicalTree?.Invoke(this, e); - foreach (var child in LogicalChildren.OfType()) - { - child.OnDetachedFromLogicalTree(e); - } + foreach (var child in LogicalChildren.OfType()) + { + child.OnDetachedFromLogicalTree(e); + } - if (((InccDebug)_classes).GetCollectionChangedSubscribers()?.Length > 0) - { - System.Diagnostics.Debugger.Break(); + if (((InccDebug)_classes).GetCollectionChangedSubscribers()?.Length > 0) + { + // TODO: This should be output using a standard logging mechanism. + System.Diagnostics.Debug.WriteLine( + $"{this.GetType().Name} detached from logical tree but still has class listeners"); + } } }