为绑定访问可观察集合中的嵌套列表



我有响应对象被添加到我的控制器如下

public void AddRoutes(IRestResponse response)
{
    ScheduleDownloadResponse json =
    JsonConvert.DeserializeObject<ScheduleDownloadResponse(response.Content);
    ScreenController.Instance.route = new
    ObservableCollection<ScheduleRoute>();
    ScreenController.Instance.vehicle = new
    ObservableCollection<Vehicle>();
    ScreenController.Instance.pickups = new
    ObservableCollection<Pickup>();
    ScreenController.Instance.compartment = new
    ObservableCollection<Compartment>();
    foreach (var item in json.Routes)
    {
        Debug.WriteLine(json.Routes);
        ScreenController.Instance.route.Add(item);
        foreach(var vehicle in item.Vehicles)
        {
            ScreenController.Instance.vehicle.Add(vehicle);
            foreach (var compartment in vehicle.Compartments)
            {
                ScreenController.Instance.compartment.Add(compartment);
            }
        }

在scheduleoute内部是其他响应对象,例如。车辆包含隔间对象。拾取包含供应商对象。所有的反应对象在自己的可观察集合

"Routes": [
 {
 "Id": 1,
 "Date": "2015-03-04T00:00:00",
 "Area": "2887",
 "Detail": "",
 "Driver": "",
 "SampleType": "",
 "Schedule": "A",
 "Tag": "",
 "Vehicles": [
  {
    "VehicleId": 22,
     "VehicleRego": "RRK160",
     "Primary": true,
     "Compartments": [
      {
        "Id": "1",
        "Capacity": 8000.0,
        "Product": ""
      }
]
},
"Pickups": [
{
     "Quantity": 6300.0,
     "Id": 1,
    "Vat": 0,
    "Hazards": "",
    "Latitude": "-34.33000000000",
    "Longitude": "-63.29119000000",
    "Message": "",
    "Supplier": {
    "Id": 1134,
    "Name": "GUTIERREZ REMO O. Y RODOLFO S."
 },
"Product": "Milk",
"ProductKey": "2",
 "Ticket": "",
 "EstmatedPickup": "2015-03-04T20:00:00+13:00"
 },

在视图中,我需要显示路由id,日期,时间表,区域和车辆编号和供应商编号

在我的视图模型中,我有一个Schedule,它是原始的副本collection from my Screen controller

Schedule = new ObservableCollection<ScheduleRoute>();
Schedule = ScreenController.Instance.route;

我在Xamarin写一个移动pcl应用程序,在Xaml中我绑定到Schedule,可以显示所有的第一级属性在scheduleoute但不知道如何得到车辆。VehicleRego和Vehicles.Suppliers.Id

作为c#的新手,我不确定如何从scheduleoute或将3个集合对象(scheduleoute, Vehicle和Supplier)组合到检索值

有什么建议吗?

因为您有Vehicles中的Collections,您需要从中选择一个。您可以提供Where()子句,获取First()Last()结果,或者直接选择。假设你有一个route,一个pickup和一个vehicle Id:

public void GetInfo(Route route, Pickup pickup, int vehicleId)
{
    Console.WriteLine(route.Id);
    Console.WriteLine(route.Date);
    Console.WriteLine(route.Schedule);
    var vehicle = route.Vehicles.Where(v => v.vehicleId == vehicleId).FirstOrDefault();
    if (vehicle != null)
    {
        Console.WriteLine(vehicle.VehicleRego);
    }
    Console.WriteLine(pickup.Supplier.Id);
}

除非您在route, pickup, suppliervehicle之间有循环引用,否则无法"猜测"信息。你必须像我一样提供它们

相关内容

  • 没有找到相关文章

最新更新