遍历 4 个对象列表并将它们的数据添加到 ListView C# Xamarin.Forms
Loop through 4 object lists & add their data in a ListView C# Xamarin.Forms
我正在做一个 Xamarin.Forms 项目,但我正处于某种死胡同。我的问题是我想在列表视图中显示我从服务器拉取的用户事务,但是我需要四个不同的拉取请求来获取所有数据,这意味着我有四个不同的对象列表,我按照你的事务编号分组可以在这个截图中看到:
The key transaction number can be seen and if you expand you'll see the other data within each transaction
这是我使用公共键对反序列化的 json 列表进行分组的代码:
var t = JsonConvert.DeserializeObject<List<trans_mod>>(transactions);
var l = JsonConvert.DeserializeObject<List<loc_mod>>(loc);
var d = JsonConvert.DeserializeObject<List<disc_mod>>(disc);
var it = JsonConvert.DeserializeObject<List<item_mod>>(itm);
var q = it.AsQueryable().GroupBy(g => g.trans).ToList();
var q2= d.AsQueryable().GroupBy(g => g.trans).ToList();
var q3 = l.AsQueryable().GroupBy(g => g.trans).ToList();
var q4 = t.AsQueryable().GroupBy(g => g.position).ToList();
每个列表的对象模型
public class loc_mod
{
[DataMember]
public string location { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
public class disc_mod
{
[DataMember]
public string discount { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
public class item_mod
{
[JsonProperty(PropertyName = "item.price")]
public string price { get; set; }
[JsonProperty(PropertyName = "item.name")]
public string name { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
public class trans_mod
{
[DataMember]
public string refer { get; set; }
[DataMember]
public string date { get; set; }
[DataMember]
public string time { get; set; }
[DataMember]
public int points { get; set; }
[DataMember]
public string _total { get; set; }
[JsonProperty(PropertyName = "$$position")]
public string position { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
public class itms
{
public string price { get; set; }
public string name { get; set; }
public DateTime stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
我想做的是遍历所有四个列表并将每个列表中的数据添加到列表视图中,但我想不出可以做到这一点的方法。
Listview Add() 代码示例:
Transactions.Add(new Transaction
{
Details = "Date: " + ti[i].date + " | Time: " + ti[i].time + " |
Reference: " + ti[i].refer,
Isvisible = false, Items= ti[i].item, Total = ti[i].total, Discount
= ti[i].discount
});
抱歉,如果这有点令人困惑,这让我感到困惑,而且我是一个相对初学者。欢迎任何帮助!
定义一个 Interface
,您的项目 类 全部实现。
该接口有一个方法 returns 列表视图所需的任何内容。
public Interface IHasTransaction
{
Transaction GetTransaction();
}
public class loc_mod : IHasTransaction
{
...
public Transaction GetTransaction()
{
// Use fields of this class to create a Transaction.
return new Transaction(...);
}
}
public class disc_mod : IHasTransaction
{
...
}
如果需要,您可以制作一个包含以下各项的列表:
public List<IHasTransaction> models = new List<IHasTransaction>();
models.Add(new loc_mod(...));
models.Add(new disc_mod(...));
给出这些项目中的任何一项
IHasTransaction model
您可以轻松获取对应的交易:
model.GetTransaction()
或
var lm = new loc_mod(...);
lm.GetTransaction()
我正在做一个 Xamarin.Forms 项目,但我正处于某种死胡同。我的问题是我想在列表视图中显示我从服务器拉取的用户事务,但是我需要四个不同的拉取请求来获取所有数据,这意味着我有四个不同的对象列表,我按照你的事务编号分组可以在这个截图中看到:
The key transaction number can be seen and if you expand you'll see the other data within each transaction
这是我使用公共键对反序列化的 json 列表进行分组的代码:
var t = JsonConvert.DeserializeObject<List<trans_mod>>(transactions);
var l = JsonConvert.DeserializeObject<List<loc_mod>>(loc);
var d = JsonConvert.DeserializeObject<List<disc_mod>>(disc);
var it = JsonConvert.DeserializeObject<List<item_mod>>(itm);
var q = it.AsQueryable().GroupBy(g => g.trans).ToList();
var q2= d.AsQueryable().GroupBy(g => g.trans).ToList();
var q3 = l.AsQueryable().GroupBy(g => g.trans).ToList();
var q4 = t.AsQueryable().GroupBy(g => g.position).ToList();
每个列表的对象模型
public class loc_mod
{
[DataMember]
public string location { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
public class disc_mod
{
[DataMember]
public string discount { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
public class item_mod
{
[JsonProperty(PropertyName = "item.price")]
public string price { get; set; }
[JsonProperty(PropertyName = "item.name")]
public string name { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
public class trans_mod
{
[DataMember]
public string refer { get; set; }
[DataMember]
public string date { get; set; }
[DataMember]
public string time { get; set; }
[DataMember]
public int points { get; set; }
[DataMember]
public string _total { get; set; }
[JsonProperty(PropertyName = "$$position")]
public string position { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
public class itms
{
public string price { get; set; }
public string name { get; set; }
public DateTime stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
我想做的是遍历所有四个列表并将每个列表中的数据添加到列表视图中,但我想不出可以做到这一点的方法。
Listview Add() 代码示例:
Transactions.Add(new Transaction
{
Details = "Date: " + ti[i].date + " | Time: " + ti[i].time + " |
Reference: " + ti[i].refer,
Isvisible = false, Items= ti[i].item, Total = ti[i].total, Discount
= ti[i].discount
});
抱歉,如果这有点令人困惑,这让我感到困惑,而且我是一个相对初学者。欢迎任何帮助!
定义一个 Interface
,您的项目 类 全部实现。
该接口有一个方法 returns 列表视图所需的任何内容。
public Interface IHasTransaction
{
Transaction GetTransaction();
}
public class loc_mod : IHasTransaction
{
...
public Transaction GetTransaction()
{
// Use fields of this class to create a Transaction.
return new Transaction(...);
}
}
public class disc_mod : IHasTransaction
{
...
}
如果需要,您可以制作一个包含以下各项的列表:
public List<IHasTransaction> models = new List<IHasTransaction>();
models.Add(new loc_mod(...));
models.Add(new disc_mod(...));
给出这些项目中的任何一项
IHasTransaction model
您可以轻松获取对应的交易:
model.GetTransaction()
或
var lm = new loc_mod(...);
lm.GetTransaction()