public bool RemoveVisualElement(SurfacePair pair) { // we don't allow the root element to be removed. if (pair.UIWidget.Parent == null) { return(false); } var parentId = pair.UIWidget.Parent.Id; var parentPair = this[parentId]; if (parentPair == null) { return(false); } // this is the only child of the content page if (parentId == Root.Id) { var page = parentPair.VisualElement as ContentPage; if (page != null) { page.Content = null; return(true); } } // this maybe a layout control var viewContainer = parentPair.VisualElement as IViewContainer <View>; var childView = pair.VisualElement as View; var parentView = parentPair.VisualElement; // we can only remove view types; this shouldn't happen. if (childView == null) { return(false); } // is this a content property? if (viewContainer == null && ElementHelper.HasContentProperty(parentView)) { ElementHelper.ClearContentProperty(parentView); return(true); } // is it a layout control? i.e. stacklayout, grid.. if (viewContainer == null) { return(false); } // made it this far, it's a layout control. viewContainer.Children.Remove(childView); return(true); }
protected override UIWidget BuildTree(Element childView, UIWidget parentWidget) { /* * 1. Return parent widget if child view is null. * Since, We won't be able to create a UIWidget out of the view. */ if (childView == null) { return(parentWidget); } /* * 2. Create a UIWidget for the child view. */ var childWidget = CreateUIWidget(childView, parentWidget); parentWidget.Children.Add(childWidget); if (ElementHelper.HasContentProperty(childView)) { childWidget.AllowsManyChildren = ElementHelper.ContentPropertyAllowsManyChildren(childView); childWidget.IsContentPropertyViewType = ElementHelper.IsContentPropertyView(childView); childWidget.ContentPropertyTypeName = ElementHelper.GetContentPropertyTypeName(childView); childWidget.HasContentProperty = true; childWidget.IsLayout = false; } else { childWidget.HasContentProperty = false; } /* * 3. Determine if the child view has children of it's own. */ var isParentView = childView as ILayoutController; if (isParentView != null) { childWidget.IsLayout = true; } // ListView check IReadOnlyList <Element> grandChildren; var isListView = childView as ITemplatedItemsView <Cell>; if (isListView == null) { grandChildren = isParentView?.Children; } else { grandChildren = isListView.TemplatedItems; } // TableView check if (grandChildren == null && childView is TableView) { var tableView = childView as TableView; var cells = new List <Element>(); foreach (var section in tableView.Root) { foreach (var cell in section) { cells.Add(cell); } } grandChildren = cells.ToArray(); } // no children if (grandChildren == null || grandChildren.Count == 0) { return(parentWidget); } /* * 4. The child does have it's own children. */ foreach (var grandChild in grandChildren) { if (grandChild != null) { if (grandChild is ILayoutController) { BuildTree(grandChild, childWidget); } else if (grandChild is ListView) { BuildTree(grandChild, childWidget); } else if (grandChild is TableView) { var grandTable = grandChild as TableView; var tableWidget = CreateUIWidget(grandChild, childWidget); childWidget.Children.Add(tableWidget); foreach (var section in grandTable.Root) { foreach (var cell in section) { BuildTree(cell, tableWidget); } } } else if (grandChild is ViewCell) { var grandViewCell = grandChild as ViewCell; var grandViewCellWidget = CreateUIWidget(grandChild, childWidget); childWidget.Children.Add(grandViewCellWidget); if (grandViewCell.View != null) { BuildTree(grandViewCell.View, grandViewCellWidget); } } else { var grandChildWidget = CreateUIWidget(grandChild, childWidget); if (ElementHelper.HasContentProperty(grandChild)) { grandChildWidget.IsLayout = true; } childWidget.Children.Add(grandChildWidget); } } } return(parentWidget); }