我正在做一个Xamarin.Forms项目,我处于一个死胡同。我的问题是,我想在列表视图中显示我从服务器中提取的用户事务,但是我需要四个不同的拉请求来获取所有数据,这意味着我有四个不同的对象列表,我按事务号分组,如您在这个屏幕截图中看到的那样:
下面是用公共键对反序列化的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
});对不起,如果这有点混乱,这对我和我都是一个相对初学者。欢迎任何帮助!
发布于 2021-07-28 18:39:11
定义一个项目类都实现的Interface。
该接口有一个返回列表视图所需内容的方法。
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()https://stackoverflow.com/questions/68564385
复制相似问题