2016-09-22 74 views
-1

我正在为UWP编写一个应用程序。转换ObservableCollection(C#uwp)

我试图根据这个答案link使用数据绑定。

这里是我的课:

public class Billing 
    { 
     public string first_name { get; set; } 
     public string last_name { get; set; } 
     public string company { get; set; } 
     public string address_1 { get; set; } 
     public string address_2 { get; set; } 
     public string city { get; set; } 
     public string state { get; set; } 
     public string postcode { get; set; } 
     public string country { get; set; } 
     public string email { get; set; } 
     public string phone { get; set; }   
    } 

    public class Shipping 
    { 
     public string first_name { get; set; } 
     public string last_name { get; set; } 
     public string company { get; set; } 
     public string address_1 { get; set; } 
     public string address_2 { get; set; } 
     public string city { get; set; } 
     public string state { get; set; } 
     public string postcode { get; set; } 
     public string country { get; set; } 
    } 

    public class RootObject 
    { 
     public int id { get; set; } 
     public int parent_id { get; set; } 
     public string status { get; set; } 

     public string order_key { get; set; } 
     public string currency { get; set; } 
     public string version { get; set; } 
     public bool prices_include_tax { get; set; } 
     public string date_created { get; set; } 
     public string date_modified { get; set; } 
     public int customer_id { get; set; } 
     public double discount_total { get; set; } 
     public double discount_tax { get; set; } 
     public double shipping_total { get; set; } 
     public double shipping_tax { get; set; } 
     public double cart_tax { get; set; } 
     public double total { get; set; } 
     public double total_tax { get; set; } 
     public Billing billing { get; set; } 
     public Shipping shipping { get; set; } 
     public string payment_method { get; set; } 
     public string payment_method_title { get; set; } 
     public string transaction_id { get; set; } 
     public string customer_ip_address { get; set; } 
     public string customer_user_agent { get; set; } 
     public string created_via { get; set; } 
     public string customer_note { get; set; } 
     public string date_completed { get; set; } 
     public string date_paid { get; set; } 
     public string cart_hash { get; set; } 
     public List<object> line_items { get; set; } 
     public List<object> tax_lines { get; set; } 
     public List<object> shipping_lines { get; set; } 
     public List<object> fee_lines { get; set; } 
     public List<object> coupon_lines { get; set; } 
    } 

    public ObservableCollection<RootObject> Orders { get; set; } 

下面是代码:

List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(products); 
foreach (RootObject root in rootObjectData) 
{ 
    string date = root.date_created; 
    string name = root.billing.first_name + root.billing.last_name ; 
    Orders = new ObservableCollection<RootObject> { new RootObject { date_created = date,billing = name } }; 
} 

随着billing = name我有这样的错误:Cannot implicitly convert type 'string' to 'Milano.InWork.Billing'

如何解决这个问题? 也许这很简单,但我找不到解决方案。 感谢您的帮助!

+0

您正试图为RootObject分配一个字符串值。当然这是行不通的。 –

+0

好吧,我该如何修复它?@ Pierre-LoupPagniez – Eugene

+0

在我看来,您尝试反序列化的JSON数据存在一些问题。 JSON根目录上“billing”属性的内容是什么? –

回答

1

With billing = name I have this error: Cannot implicitly convert type 'string' to 'Milano.InWork.Billing'

当然会发生这种错误,你billing班上RootObject的数据类型为Billing,这是你定义的类。

刚刚从你的代码,我觉得你只是想显示first_namelast_name作为字符串中的root.billing,那么您可以在您的RootObject类更改代码public Billing billing { get; set; }public string billing { get; set; }

相关问题