Xamarin表单-如何使用ListVIew [英] Xamarin Forms - How To Use A ListVIew
问题描述
我正在尝试使用我创建的自定义单元格填充列表视图.我知道如何使用tableview,但我不知道如何使用listview.有人可以向我解释,谢谢
I am trying to populate a listview with a customcell I made. I know how to do it with a tableview, but i have no idea how to do it with a listview. Can someone explain to me please, thank you
推荐答案
对不起,这段代码很粗糙,但这仅仅是为了解释列表视图和数据绑定背后的概念.
Excuse the very rough snippet of code but it is merely to explain the concept behind listviews and databinding.
在下面的示例中,我创建一个列表视图,分配一个自定义viewCell并将该视图单元绑定到一个模型.模型数据由从服务器检索到的XML数据填充(为简单起见,发出了代码).如果您需要包含硬编码数据的列表视图,则可以实现更简单的实现.
In my example below I create a listview, assign a custom viewCell and bind the view cell to a model. The model data is populated by XML data retrieved from the server(Code emitted for simplicity). Simpler implementations can be done should you require a listview with hardcoded data.
我建议您仔细阅读 http://developer.xamarin.com/guides/cross-platform/xamarin-forms/introduction-to-xamarin-forms/并研究MVVM原理
I suggest reading through the http://developer.xamarin.com/guides/cross-platform/xamarin-forms/introduction-to-xamarin-forms/ and researching the MVVM principle
另外,我发现最好的示例代码是James Montemagno编写的启发性应用程序 https://github.com/jamesmontemagno/Hanselman.Forms
Additionally the best example code I have found to go through is an enlightening application written by James Montemagno https://github.com/jamesmontemagno/Hanselman.Forms
public TestPage()
{
private ListView listView; // Create a private property with the Type of Listview named listview
listView = new ListView(); //Instantiate the listview
var viewTemplate = new DataTemplate(typeof(CustomViewCell)); //Create a variable for the custom data template
listView.ItemTemplate = viewTemplate; // set the data template to the variable created above
this.Content = new StackLayout()
{
Children = {
listView
}
};
}
自定义视图单元格:
public class CustomViewCell : ViewCell
{
public CustomViewCell()
{
Label Test = new Label()
{
Font = Font.BoldSystemFontOfSize(17),
TextColor = Helpers.Color.AppGreen.ToFormsColor(),
BackgroundColor = Color.White
};
var image = new Image();
image.Source = ImageSource.FromFile("testing.png");
Label Test2 = new Label();
tour_Desc.SetBinding(Label.TextProperty, "Test");
round_Desc.SetBinding(Label.TextProperty, "Test2");
var grid = new Grid
{
VerticalOptions = LayoutOptions.FillAndExpand,
RowDefinitions =
{
new RowDefinition {Height = GridLength.Auto },
new RowDefinition {Height = GridLength.Auto },
new RowDefinition {Height = 1}
},
ColumnDefinitions =
{
new ColumnDefinition {Width = GridLength.Auto},
new ColumnDefinition {Width = new GridLength(1, GridUnitType.Star)},
new ColumnDefinition {Width = 80 }
}
,BackgroundColor = Color.White,
};
grid.Children.Add(image, 0, 0);
Grid.SetRowSpan(image, 2);
grid.Children.Add(tour_Desc, 1, 0);
Grid.SetColumnSpan(tour_Desc, 2);
grid.Children.Add(Test, 1, 1);
grid.Children.Add(Test2, 1, 2);
this.View = grid;
}
}
查看模型:
public partial class GamesResult
{
public string Test { get; set; }
public string Test1 { get; set; }
}
这篇关于Xamarin表单-如何使用ListVIew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!