在C#或Xamarin中列出对象

时间:2014-10-10 21:18:19

标签: c# xamarin

我正在尝试存储所有Customer对象属性以传递选定的listitem以在详细视图中显示selectedItem信息。我成功从webservice获取所有客户信息。但是,在我的for循环中,我收到以下错误。

    List <string> list= new List<string>();
    private IEnumerable <IDictionary<string,object>> myData;

    foreach (var customers in myData) {
                list.Add(new Customer((string)customers["CustomerID"],
                    (string)customers ["CompanyName"],
                    (string)customers ["ContactName"],
                    (string)customers ["ContactTitle"],
                    (string)customers ["Address"],
                    (string)customers ["Region"],
                    (string)customers ["PostalCode"],
                    (string)customers ["City"],
                    (string)customers ["Fax"],
                    (string)customers ["Phone"]
                ));

            }
        listView.ItemsSource=list;


public Customer (string customerID,string companyName, 
        string contactName, string contactTitle, string address, string region, string postalCode,string city, string fax, string phone)
    {
        CustomerID = customerID;
        CompanyName = companyName;
        ContactName = contactName;
        ContactTitle = contactTitle;
        Address = address;
        Region = region;
        PostalCode = postalCode;
        City = City;
        Fax = fax;
        Phone = phone;
    }

enter image description here

2 个答案:

答案 0 :(得分:1)

在您的代码中,您的List类型是字符串。您所要做的就是将List类型字符串更改为Customer。如下所示

List <Customer> list = new List<Customer>();

var list = new List<Customer>();

答案 1 :(得分:0)

最初,我只是在我的列表中添加字符串然后我决定收集对象级别,然后我忘了从字符串更改为对象。

List <string> list= new List<string>(); 

以下代码段

List <Customer> list= new List<Customer>();

我没有注意。