2016-09-22 2 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? –

ответ

1

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

Конечно будет происходить эта ошибка, ваш billing в классе RootObject имеет тип данных Billing, который является еще один класс вы определили.

Просто из вашего кода я думаю, что вы только хотите, чтобы показать first_name и last_name как строка в вашем root.billing, то вы можете изменить код public Billing billing { get; set; } в вашем RootObject класса public string billing { get; set; }.

Смежные вопросы