Browse Source

Check for correct state on attach/detach to tree.

Make sure that if we're attached to a logical tree, we can invoke
DetachedFromLogicalTree when Parent is set to null. This also has a side
effect in that Parent will now be non-null when DetachedFromLogicalTree
is called.
pull/467/head
Steven Kirk 11 years ago
parent
commit
766a814f4e
  1. 1
      src/Perspex.Base/Perspex.Base.csproj
  2. 22
      src/Perspex.Base/PerspexInternalException.cs
  3. 28
      src/Perspex.Controls/Control.cs

1
src/Perspex.Base/Perspex.Base.csproj

@ -63,6 +63,7 @@
<Compile Include="Metadata\DependsOnAttribute.cs" />
<Compile Include="Metadata\ContentAttribute.cs" />
<Compile Include="PerspexDisposable.cs" />
<Compile Include="PerspexInternalException.cs" />
<Compile Include="PerspexLocator.cs" />
<Compile Include="Metadata\XmlnsDefinitionAttribute.cs" />
<Compile Include="PerspexObjectExtensions.cs" />

22
src/Perspex.Base/PerspexInternalException.cs

@ -0,0 +1,22 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
namespace Perspex
{
/// <summary>
/// Exception signifying an internal logic error in Perspex.
/// </summary>
public class PerspexInternalException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="PerspexInternalException"/> class.
/// </summary>
/// <param name="message">The exception message.</param>
public PerspexInternalException(string message)
: base(message)
{
}
}
}

28
src/Perspex.Controls/Control.cs

@ -391,22 +391,32 @@ namespace Perspex.Controls
throw new InvalidOperationException("The Control already has a parent.");
}
InheritanceParent = parent as PerspexObject;
_parent = (IControl)parent;
if (_isAttachedToLogicalTree)
{
var oldRoot = FindStyleRoot(old);
var root = FindStyleRoot(old);
if (oldRoot == null)
{
throw new PerspexInternalException("Was attached to logical tree but cannot find root.");
}
if (root != null)
{
var e = new LogicalTreeAttachmentEventArgs(root);
var e = new LogicalTreeAttachmentEventArgs(oldRoot);
OnDetachedFromLogicalTree(e);
}
root = FindStyleRoot(this);
InheritanceParent = parent as PerspexObject;
_parent = (IControl)parent;
if (root != null)
if (_parent is IStyleRoot || _parent?.IsAttachedToLogicalTree == true)
{
var e = new LogicalTreeAttachmentEventArgs(root);
var newRoot = FindStyleRoot(this);
if (newRoot == null)
{
throw new PerspexInternalException("Parent is atttached to logical tree but cannot find root.");
}
var e = new LogicalTreeAttachmentEventArgs(newRoot);
OnAttachedToLogicalTree(e);
}

Loading…
Cancel
Save