【问题标题】:How to binding Data Listview in Xamarin C#如何在 Xamarin C# 中绑定数据列表视图
【发布时间】:2019-05-14 06:11:26
【问题描述】:

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。如何制作。谢谢大家收看。

【问题讨论】:

  • 我建议你从一个使用 TextCell 的简单 ListView 开始 - 首先让它工作。然后扩展它以使用具有更复杂布局的 ViewCell。
  • 您似乎完全混淆了创建视图的 XAML 和 CS 方法。我的建议是回去仔细阅读文档。

标签: listview xamarin data-binding xamarin.forms


【解决方案1】:

您在public WithDataTemplatePageCS() 方法中错过了InitializeComponent();。把它放在那个方法下的第一行。

【讨论】:

    猜你喜欢
    • 2015-04-22
    • 2023-03-12
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多