当我尝试循环网格时,没有错误。但是,当我运行progarm时,它会崩溃XamlParseException
,TypeInitializationException
并且InvalidCastException
它正常工作,直到我添加最后一行。在此之后,即使我还原了所有更改,程序也从未运行过。请帮我。
foreach(Control c in this.MainGrid.Children)
{
if(c.Name.Contains("Name")) NameLabels.Add((Label)c);
}
public static List<Label> NameLabels = new List<Label>();
发布于 2019-05-17 15:54:52
我不知道我是否理解,但你可以试试这个:
List<Label> _list = new List<Label>();
foreach (Control c in Controls)
{
if(c.GetType() == typeof(Label))
{
if (c.Name.Contains("Name"))
_list.Add((Label)c);
}
}
发布于 2019-05-17 17:04:30
您可能在MainGrid中有一些无法转换为控件的东西。
public static List<Label> NameLabels = new List<Label>();
foreach(object c in MainGrid.Children)
{
if(c.GetType() == typeof(Label))
{
Label label = c as Label;
if(label.Name.Contains("Name")) NameLabels.Add(label);
}
}
您还应该看看最小的,可重现的示例。
https://stackoverflow.com/questions/-100006748
复制相似问题