【问题标题】:Convert in ObservableCollection (C# uwp)在 ObservableCollection 中转换 (C# uwp)
【发布时间】:2016-10-01 04:30:40
【问题描述】:

我正在为 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'

如何解决此错误? 也许这很简单,但我没有找到解决方案。 感谢您的帮助!

【问题讨论】:

  • 您正在尝试将字符串值分配给 RootObject。这当然行不通。
  • 好的,我该如何解决?@Pierre-LoupPagniez
  • 在我看来,您尝试反序列化的 JSON 数据存在一些问题。 JSON 根目录上的“计费”属性的内容是什么?
  • 例如"billing": { "first_name": "John", "last_name": "Doe", "company": "", "address_1": "969 Market", "address_2": "", "city": "San Francisco", "state": "CA", "postcode": "94103", "country": "US", "email": "john.doe@example.com", "phone": "(555) 555-5555" },@AldineiSampaio
  • 结果会是什么样子?当您尝试将root.billing.first_name + root.billing.last_name 设置为计费对象时?喜欢Billing billing = "JohnDoe";

标签: c# data-binding uwp


【解决方案1】:

使用 billing = name 我有这个错误:无法将类型 'string' 隐式转换为 'Milano.InWork.Billing'

当然会出现这个错误,你在RootObject类中的billing的数据类型是Billing,这是你定义的另一个类。

仅从您的代码中,我认为您只想在root.billing 中将first_namelast_name 显示为字符串,然后您可以将RootObject 类中的代码public Billing billing { get; set; } 更改为public string billing { get; set; }

【讨论】:

    猜你喜欢
    • 2017-08-06
    • 1970-01-01
    • 2017-11-28
    • 2019-01-18
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2020-01-26
    相关资源
    最近更新 更多