简体   繁体   English

Layout如何在Xamarin.Forms中绘制其子元素?

[英]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. 构建自己的视图的传统方法似乎是从View派生一个新类,然后为它提供特定平台的渲染器。 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? 我想知道,有没有办法仅使用Element类来重现此行为? 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. 从理论上讲,它应该具有建立正确的父子关系的方法,但是当我尝试设置Parent属性时,子元素不会显示在父级的表面上。 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. 有一个名为LogicalChildrenInternal的属性,负责枚举每个元素的子级。 Unfortunately, it is internal virtual and returns a static empty collection, so the Element class is not suitable for direct use. 不幸的是,它是内部虚拟的,并返回一个静态的空集合,因此Element类不适合直接使用。 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. 在诸如ViewCell和Layout的类中,此属性被覆盖,您可以通过反射替换原始集合,并看到新的set子集显示在布局上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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