简体   繁体   English

通过Xamarin.IOS中的列表值创建RadioElement列表

[英]Create a list of RadioElement via the values of a list in Xamarin.IOS

I want to create a list of RadioElement via the values of an ArrayList. 我想通过ArrayList的值创建RadioElement列表。 Imagine I have a list: 想象一下,我有一个列表:

System.Collections.Generic.List<MyClass> mylist

Now I don't want to iterate through all elements and create a RadioElement for everyone. 现在,我不想遍历所有元素并为每个人创建一个RadioElement Is it possible to create the RadioElements automatically by a property of MyClass , by passing the List<MyClass> . 是否可以通过传递List<MyClass>MyClass的属性自动创建RadioElements And how can I get the Tapped Event when the user selects one of the RadioElements ? 我怎样才能得到Tapped事件,当用户选择的一个RadioElements

Linq to the rescue: https://msdn.microsoft.com/en-us/library/bb397900.aspx Linq进行救援: https : //msdn.microsoft.com/zh-cn/library/bb397900.aspx

As an example, I created a MyClass with a bool property CreateRadioElement and an ElementText string property: 例如,我创建了一个MyClass,它具有一个bool属性CreateRadioElement和一个ElementText字符串属性:

public class MyClass
{
    public bool CreateRadioElement { get; set; }

    public string ElementText { get; set; } 
}

Then I created a List of MyClass objects: 然后,我创建了MyClass对象的列表:

List<MyClass> elements = new List<MyClass>();
elements.Add(new MyClass { CreateRadioElement = true, ElementText = "1" });
elements.Add(new MyClass { CreateRadioElement = false, ElementText = "2" });
elements.Add(new MyClass { CreateRadioElement = true, ElementText = "3" });
elements.Add(new MyClass { CreateRadioElement = false, ElementText = "4" });
elements.Add(new MyClass { CreateRadioElement = true, ElementText = "5" });

So now we only want the MyClass objects with CreateRadioElement set to true: 因此,现在我们只希望将CreateRadioElement设置为true的MyClass对象:

IEnumerable<MyClass> radioelementsQuery = from element in elements
                                          where element.CreateRadioElement == true
                                          select element;

Now you execute the query with a foreach loop: 现在,您使用foreach循环执行查询:

foreach (MyClass mc in radioelementsQuery)
{
      Console.WriteLine("Element: {0}", mc.ElementText);
}

And you will only see 1, 3, and 5 in the console output. 而且您将在控制台输出中仅看到1、3和5。 And of course instead of writing to the console, you could assign each to RadioElement. 当然,除了将其写入控制台之外,您还可以将它们分配给RadioElement。

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

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