Ejemplo n.º 1
0
        public void AddChild(UIElement child, int?index = null)
        {
            if (child == null)
            {
                return;
            }

            var currentParent = child.GetParent() as UIElement;

            // Remove child from current parent, if any
            if (currentParent != this && currentParent != null)
            {
                // ---IMPORTANT---
                // This behavior is different than UWP:
                // On UWP the behavior would be to throw an "Element already has a logical parent" exception.

                // It is done here to align Wasm with Android and iOS where the control is
                // simply "moved" when attached to another parent.

                // This could lead to "child kidnapping", like the one happening in ComboBox & ComboBoxItem

                this.Log().Info($"{this}.AddChild({child}): Removing child {child} from its current parent {currentParent}.");
                currentParent.RemoveChild(child);
            }

            child.SetParent(this);
            OnAddingChild(child);

            if (index is int actualIndex && actualIndex != _children.Count)
            {
                var currentVisual = _children[actualIndex];
                _children.Insert(actualIndex, child);
                Visual.Children.InsertAbove(child.Visual, currentVisual.Visual);
            }
Ejemplo n.º 2
0
        internal UIElement RemoveChild(UIElement child)
        {
            _children.Remove(child);
            child.SetParent(null);

            if (Visual != null)
            {
                Visual.Children.Remove(child.Visual);
            }

            return(child);
        }