麻烦迭代元素的迭代

时间:2017-01-11 17:24:37

标签: xamarin.forms

因此,只需尝试以编程方式迭代特定的stacklayout的子节点,并获取这些元素的子节点。我迭代的第一个元素,给了我一个Children对象的选项。就这样:

var TextContainerChildren = TextContainer.Children;

TextContainer是一个StackLayout,它包含许多其他stacklayout。就这样:

<StackLayout x:Name="TextContainer" Padding="10,10,10,0">
              <Label Text="What's New" FontSize="20" />
              <!--<StackLayout Orientation="Vertical" Padding="0,10,0,10">
                <Label Font="15" Text="Version 2.3.1 - January 9, 2017" TextColor="Blue" />
                <Label Font="15" Text=" &#8226; Breaking News Push Notifications. Users can now subscribe to breaking news push notifications that display an alert on your phone when a breaking news story is published." />
                <Label x:Name="LabelEmailHelp" Font="15" Text="" />
              </StackLayout>-->

              <StackLayout Orientation="Vertical" Padding="0,10,0,10">
                <Label Font="15" Text="Version 2.3.1 - January 12, 2017" TextColor="Blue" />
                <Label Font="15" Text=" &#8226; Minor bug fixes." />
                <Label x:Name="LabelEmailHelp" Font="15" Text="" />
              </StackLayout>
</StackLayout>

所以现在我尝试迭代TextContainer.Children

foreach(Xamarin.Forms.View el in TextContainerChildren)
{
    if(el.GetType() == typeof(Xamarin.Forms.StackLayout))
    {
        var j = el;
    }
}

问题是,el不允许我访问孩子。 (尝试做el.Children并不起作用,我的意思是,当我在调试中查看对象时,没有Children)。但是,当我调试时,我可以看到el是StackLayout,而在base我看到了Children对象。

有没有办法访问元素的base?如果没有,任何人都知道如何读取迭代元素的子元素?

1 个答案:

答案 0 :(得分:2)

你需要施展它

if(el.GetType() == typeof(Xamarin.Forms.StackLayout))
{
    StackLayout j = (StackLayout)el;

    // now you can use j.Children
}