Как привязать Data Listview в Xamarin C # - PullRequest
0 голосов
/ 13 декабря 2018

XAML

<?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, но он не работает.

Наконец: я хочу привязать динамический массив данных в коде к XAML.Как это сделать.Спасибо всем, что вы смотрите.

1 Ответ

0 голосов
/ 14 декабря 2018

Вы пропустили InitializeComponent(); в вашем public WithDataTemplatePageCS() методе.Поместите его в первую строку под этим методом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...