Xamarin Forms 访问每个子元素的 属性
Xamarin Forms Access every child element's Property
首先,我是初学者
我想访问堆栈布局中不同类型的每个子元素。
我尝试了但找不到任何解决方案。
我正在使用 Microsoft Visual Studio Community 2019 版本 16.11.9。
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="NewExp.MainPage"
x:Name="mainPage">
<StackLayout x:Name="first">
<StackLayout x:Name="second">
<Label Text="label1"/>
<Label Text="label2"/>
<Label Text="label3"/>
<Label Text="label4"/>
<Label Text="label5"/>
</StackLayout>
<AbsoluteLayout x:Name="third">
<Button Text="BTN" Clicked="Button_Clicked" />
</AbsoluteLayout>
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace NewExp
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
Type type;
Label label;
// I want to access each child and change there's property eg. Text
var _list = second.Children.ToList();
for (int i = 0; i < _list.Count; i++)
{
// Not working
//_list[i].Text = "New Text";
// Not working
//type = _list[i].GetType();
//var _lst = (type)_list[i];
//_lst.Text = "New Text";
// Working, but I need to know previously that it is a Label
// and this is not real world case.
// I also used Linq, but no benefit
label = (Label)_list[i];
label.Text = "New Text";
}
}
}
}
如有任何建议,我们将不胜感激。提前致谢。
正如我在评论中指出的那样,以这种方式直接修改 UI 通常不是一个好主意,通过数据绑定可以更好地实现。但是,如果您必须这样做,这样的事情应该可行
foreach (var c in second.Children)
{
if (c is Label l)
{
l.Text = "some new text";
}
// repeat for any other types
}
首先,我是初学者
我想访问堆栈布局中不同类型的每个子元素。
我尝试了但找不到任何解决方案。
我正在使用 Microsoft Visual Studio Community 2019 版本 16.11.9。
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="NewExp.MainPage"
x:Name="mainPage">
<StackLayout x:Name="first">
<StackLayout x:Name="second">
<Label Text="label1"/>
<Label Text="label2"/>
<Label Text="label3"/>
<Label Text="label4"/>
<Label Text="label5"/>
</StackLayout>
<AbsoluteLayout x:Name="third">
<Button Text="BTN" Clicked="Button_Clicked" />
</AbsoluteLayout>
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace NewExp
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
Type type;
Label label;
// I want to access each child and change there's property eg. Text
var _list = second.Children.ToList();
for (int i = 0; i < _list.Count; i++)
{
// Not working
//_list[i].Text = "New Text";
// Not working
//type = _list[i].GetType();
//var _lst = (type)_list[i];
//_lst.Text = "New Text";
// Working, but I need to know previously that it is a Label
// and this is not real world case.
// I also used Linq, but no benefit
label = (Label)_list[i];
label.Text = "New Text";
}
}
}
}
如有任何建议,我们将不胜感激。提前致谢。
正如我在评论中指出的那样,以这种方式直接修改 UI 通常不是一个好主意,通过数据绑定可以更好地实现。但是,如果您必须这样做,这样的事情应该可行
foreach (var c in second.Children)
{
if (c is Label l)
{
l.Text = "some new text";
}
// repeat for any other types
}