访问Observable Collection for Binding

时间:2015-05-19 21:55:16

标签: c# linq xaml xamarin observablecollection

我有一些响应对象被添加到我的可观察集合中 控制器如下

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);
            }
        }

ScheduleRoute内部是其他响应对象,例如。车辆包含 Compartment对象。皮卡包含供应商对象。所有回应 对象在自己的可观察集合中

"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,日期,时间表,区域和 来自车辆的VehicleRego和来自供应商的Id。

在我的View模型中,我有一份原件的副本 从我的屏幕控制器收集

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

我正在Xamarin和Xaml中编写移动pcl应用程序我绑定到Schedule并且可以显示所有第一级 scheduleRoute中的属性,但不知道如何到达Vehicles.VehicleRego和 Vehicles.Suppliers.Id

不熟悉C#我不知道如何从scheduleRoute或者访问嵌套级别 将3个集合对象(ScheduleRoute,Vehicle和Supplier)组合到 检索值

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

因为您Collections Vehicles,所以您需要选择其中一个。您可以提供Where()子句,获取First()Last()结果,或者只需选择。我们假设您有routepickupvehicle 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);
}

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