ListView的对象并将数据以Xamarin形成下一个视图

问题描述:

我已经收集了我的每一个客户的所有信息,并希望以填充我的是ListView的初步看法客户ID信息。不过,我觉得我对对象的一个​​水平上面,我不知道如何让我的ListView客户ID信息。

I have collected all the information for each of my customer and would like to populate CustomerID information on my initial view which is listView. However, I think I am on one level top of the object and I do not know how to get CustomerID information on my listView.

此外,我想选择的项目对象的信息传递给下一个视图来显示相应的客户信息的其余部分。

Also I would like to pass selected item object information to the next view to show the rest of corresponding customer information.

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

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;

SearchBar searchBar = new SearchBar
{
    Placeholder = "Xamarin.Forms Property",
};

Padding = new Thickness (10, 20, 10, 10);
Content = new StackLayout () {
    Children={searchBar,listView}
};

listView.ItemSelected += (sender, e) => {
    //my next view is DetailPage
};



在这里输入的形象描述

您的的ItemsSource 是对象的集合。

您需要创建一个的ItemTemplate ,然后将使用的为每个项目此集合内进行渲染。

You will need to create an ItemTemplate that will then be used for each item within this collection to render from.

在这个的ItemTemplate 您可以添加各种 Xamarin.Forms细胞,看到的这里,或从派生创建自己的布局 ViewCell

Within this ItemTemplate you can add various Xamarin.Forms Cells, see here, or create your own layout by deriving from ViewCell.

有是一个的ListView 使用自定义类的此处,实现页面的结束,这将是对你非常有用的两个代码隐藏 XAML 的方法。

There is a tutorial with a ListView using custom classes here, towards the end of the page that will be quite useful to you in both code-behind and XAML approaches.

这也说明如何进行选择的值,并通过这个到另一个页面,通过查看在 listView.SelectedItem ,并投射到您的模型。

It also shows how to take a selected value and pass this on to another page, by looking at the selected value in listView.SelectedItem and casting it to your model.