我有一个堆栈布局,其中包含一长串具有不同视图的子项,例如图像,标签等。
namespace bizclos1{
public class Page1 : ContentPage
{
public Button SaveLabel= new Button();
public Button SaveImage = new Button();
public StackLayout TotalStackLayout = new StackLayout();
public Page1 ()
{
SaveLabel.Clicked += SaveLabel_Clicked;
SaveImage.Clicked += SaveImage_Clicked;
Content = new StackLayout {
Children = {
SaveImage,
SaveLabel,
TotalStackLayout
}
};
}
private void SaveImage_Clicked(object sender, EventArgs e)
{
Image IMG = new Image();
TotalStackLayout.Children.Add(IMG);
}
private void SaveLabel_Clicked(object sender, EventArgs e)
{
Label LBL = new Label();
TotalStackLayout.Children.Add(LBL);
}
}
}
第一个问题
我想知道是否可以仅选择标签,请问该如何做?
第二个问题
还要如何选择特定索引的子级,如索引4的子级?
答案 0 :(得分:1)
您可以使用LINQ
仅选择Label
元素
using System.Linq;
var labels = TotalStackLayout.Children.Where(x => x is Label).ToList();
通过索引选择元素
var item = TotalStackLayout.Children[4];