简体   繁体   中英

How to access property inside property?

I have a model class like this:

public class MapModel
{
    public Field[] Fields { get; set; }
}

public class Field
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Node[] Nodes { get; set; }
    public string Icon { get; set; }
}

public class Node
{
    public float Long { get; set; }
    public float Lat { get; set; }
}

In my view model I'm calling a service to get some map data and ObservableCollection like:

public ObservableCollection<Field> FieldsCollection { get; set; } = new ObservableCollection<Field>(); 

var mapData = await MapService.GetMapData(token, "sr");

foreach (var md in mapData.Fields)
{
    FieldsCollection.Add(md);                
    PinIcon = Icon;
}

Now I'm trying to set values Long and Lat from Node for position of Pin on map...

public void LoadMapTab()
{
    foreach (var item in FieldsCollection)
    {                
        var pin = new Pin
                      {
                          Label = item.Name,
                          Position = new Position(/* SET VALUES FROM NODE LONG AND LAT*/),
                          Icon = BitmapDescriptorFactory.FromBundle(PinIcon)
                      };

        _map.Pins.Add(pin);
        Device.StartTimer(TimeSpan.FromMilliseconds(500), () =>
            {
                _map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(/* SET VALUES FROM NODE LONG AND LAT*/),
                Distance.FromKilometers(8)));
                return false;
            });
    }
}

So, my question is how to access the Long and Lat values and set them in

Position = new Position(.......)

item is a Field , so it has a Nodes array. You can use an indexer [] to specify an index in the array

Position = new Position(item.Nodes[0].Lat, item.Nodes[0].Long),

note, attribtute has a specific meaning in C#. These are properties , not attributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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