简体   繁体   中英

How does Layout draw its child elements in Xamarin.Forms?

The traditional way to build your own view seems to be deriving a new class from View and then supplying it with renderers for specific platforms. In renderers you create the visual tree of elements that are displayed for that view. However, layout classes don't seem to have renderers and yet they are capable of drawing any visual structure of elements on their surface.

I wonder, is there a way to reproduce this behavior using only the Element class? Theoretically, it should have the means to establish proper parent-child relationships, but when I try to set the Parent property, child elements don't get displayed on the parent's surface. The parent itself is displayed.

So how do I get child elements to appear on the screen?

There is a property called LogicalChildrenInternal that is responsible for enumerating each element's children. Unfortunately, it is internal virtual and returns a static empty collection, so the Element class is not suitable for direct use. In classes like ViewCell and Layout, where this property is overridden, you can substitute the original collection via reflection and see that a new set children gets displayed on the layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM