我想将来自JSON的数据绑定到xamarin.forms中的ListView。我打了电话,我已经解析了JSON的数据。现在我无法理解在xamarin.forms中绑定数据的最佳方法。
JSON:
[{"Product":"XXX","Origins":[{"Origin":"ORI1","Details":[{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}]},{"Origin":"ORI2","Details":[{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}]}]},{"Product":"PQR","Origins":[{"Origin":"ABC","Details":[{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}]},{"Origin":"PPP","Details":[{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}]}]}]
这个JSON有产品,起源,产品等细节有很多起源和来源有不同的卖家,买家等。
我的来源是我的对象类:
public class Details
{
public string Seller { get; set; }
public string Seller_Rate { get; set; }
public string Saller_QTY { get; set; }
public string Buyer { get; set; }
public string Buyer_Rate { get; set; }
public string Buyer_QTY { get; set; }
}
public class Origins
{
public string Origin { get; set; }
public List<Details> Details { get; set; }
}
public class Products
{
public List<Origins> Origins { get; set; }
public string PRODUCT { get; set; }
}
我在此方法中解析它以测试结果:
public async Task<List<Products>> getProductsJson()
{
List<Products> root=null;
Log.Debug("Login", "in get products");
string responsejson = await client.GetStringAsync(Constants.url_getproducts);
Log.Debug("Login", responsejson);
try
{
root = JsonConvert.DeserializeObject<List<Products>>(responsejson);
Log.Debug("Login",Convert.ToString(root.Count));
for(int i=0;i<root.Count;i++)
{
Log.Debug("Login", "=============================");
Log.Debug("Login", root[i].PRODUCT);
for(int j=0;j<root[i].Origins.Count;j++)
{
Log.Debug("Login"," "+root[i].Origins[j].Origin);
for(int k=0;k<root[i].Origins[j].Details.Count;k++)
{
Log.Debug("Login", " Buy " + root[i].Origins[j].Details[k].Buyer+" BUY_QTY "+ root[i].Origins[j].Details[k].Buyer_QTY);
}
}
Log.Debug("Login", "=============================");
}
//Log.Debug("Login", root[0].PRODUCT);
//Log.Debug("Login", root[0].Origins[0].Origin);
// Log.Debug("Login", root[0].Origins[0].Details[0].Seller);
}
catch(Exception e)
{
Log.Debug("Login", e.Message);
Log.Debug("Login", e.Source);
}
return root;
}
正如我在这里打印日志一样,这个日志打印得恰到好处,每件事情都正常。我在xamal页面上将此方法称为:
async void GetProduct()
{
List<Products> result = await App.RestService.getProductsJson();
list_data.ItemsSource = result;
}
这里我将结果设置为 listview 。
在设计页面上:
<ContentPage.Content>
<StackLayout VerticalOptions="FillAndExpand">
<RelativeLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<StackLayout x:Name="logoStack">
<Label x:Name="lbl_noInternet" HorizontalTextAlignment="Center" TextColor="Red" BackgroundColor="Black" />
</StackLayout>
<ListView x:Name="list_data" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<!--<TextCell Text="{Binding Topic}" TextColor="Purple" Detail="{Binding Description}" DetailColor="Black"/>-->
<!--<ImageCell Text="{Binding Topic}" TextColor="Purple" Detail="{Binding Description}" DetailColor="Black" ImageSource="{Binding ImageSource}" />-->
<ViewCell>
<ViewCell.View>
<StackLayout Spacing="5" Orientation="Horizontal" HorizontalOptions="StartAndExpand" >
<Label Text="{Binding PRODUCT}" FontSize="20" FontAttributes="Bold" />
<Label Text="{Binding Origin}" FontSize="20" HorizontalOptions="End" HorizontalTextAlignment="End" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</RelativeLayout>
</StackLayout>
</ContentPage.Content>
现在。我的代码工作正常,这意味着没有任何错误,但只显示PRODUCT,原始值没有显示在标签上。
现在我有3个问题。
3.如果我们需要以编程方式绑定它意味着从.cs文件动态地如何做到这一点??