<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:DataTemplates" x:Class="DataTemplates.WithDataTemplatePage" Title="With a Data Template" Icon="xaml.png">
<StackLayout Margin="20">
<Label Text="ListView with a Data Template" FontAttributes="Bold" HorizontalOptions="Center" />
<ListView Margin="0,20,0,0">
</ListView>
</StackLayout>
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms;
namespace DataTemplates
{
public class WithDataTemplatePageCS : ContentPage
{
public WithDataTemplatePageCS()
{
Title = "With a Data Template";
Icon = "csharp.png";
var people = new List<Person>
{
new Person { Name = "Steve", Age = 21, Location = "USA" },
new Person { Name = "John", Age = 37, Location = "USA" },
new Person { Name = "Tom", Age = 42, Location = "UK" },
new Person { Name = "Lucas", Age = 29, Location = "Germany" },
new Person { Name = "Tariq", Age = 39, Location = "UK" },
new Person { Name = "Jane", Age = 30, Location = "USA" }
};
var personDataTemplate = new DataTemplate(() =>
{
var grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.5, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.2, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.3, GridUnitType.Star) });
var nameLabel = new Label { FontAttributes = FontAttributes.Bold };
var ageLabel = new Label();
var locationLabel = new Label { HorizontalTextAlignment = TextAlignment.End };
nameLabel.SetBinding(Label.TextProperty, "Name");
ageLabel.SetBinding(Label.TextProperty, "Age");
locationLabel.SetBinding(Label.TextProperty, "Location");
grid.Children.Add(nameLabel);
grid.Children.Add(ageLabel, 1, 0);
grid.Children.Add(locationLabel, 2, 0);
return new ViewCell
{
View = grid
};
});
Content = new StackLayout
{
Margin = new Thickness(20),
Children = {
new Label {
Text = "ListView with a Data Template",
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center
},
new ListView { ItemsSource = people, ItemTemplate = personDataTemplate, Margin = new Thickness(0, 20, 0, 0) }
}
};
}
}
}
我在这里尝试过以下示例:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/creating,但它不起作用。
最后:我想将代码背后的Data Dynamic绑定到XAML。怎么做。谢谢大家的观看。
答案 0 :(得分:0)
您在InitializeComponent();
方法中错过了public WithDataTemplatePageCS()
。将其放在该方法的第一行。