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

79 Views Asked by At

I want to create a list of RadioElement via the values of an ArrayList. 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. Is it possible to create the RadioElements automatically by a property of MyClass, by passing the List<MyClass>. And how can I get the Tapped Event when the user selects one of the RadioElements?

1

There are 1 best solutions below

2
jgoldberger - MSFT On

Linq to the rescue: https://msdn.microsoft.com/en-us/library/bb397900.aspx

As an example, I created a MyClass with a bool property CreateRadioElement and an ElementText string property:

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

    public string ElementText { get; set; } 
}

Then I created a List of MyClass objects:

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:

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

Now you execute the query with a foreach loop:

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

And you will only see 1, 3, and 5 in the console output. And of course instead of writing to the console, you could assign each to RadioElement.