Convert in ObservableCollection (C# uwp)

本文关键字:uwp in ObservableCollection Convert | 更新日期: 2023-09-27 18:08:21

我正在为UWP编写一个应用程序。

我尝试根据这个答案链接使用数据绑定。

这是我的类:

 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'

如何修复这个错误?也许这很简单,但我找不到解决办法。谢谢你的帮助!!

Convert in ObservableCollection (C# uwp)

与billing = name我有这个错误:不能隐式地转换类型'string'到' milan . inwork . billing '

当然这个错误会发生,RootObject类中的billing具有Billing数据类型,这是您定义的另一个类。

只是从你的代码,我认为你只想显示first_namelast_name作为字符串在你的root.billing,然后你可以改变代码public Billing billing { get; set; }在你的RootObject类到public string billing { get; set; }