【问题标题】:Binding a property of code behind xaml page to list view将xaml页面后面的代码属性绑定到列表视图
【发布时间】:2016-08-25 04:09:22
【问题描述】:

我尝试在 xaml 页面背后的代码中使用名为“People”的公共静态属性,并将其填充到页面背后代码的构造函数中。然后在 xaml 页面中,我让该公共属性绑定到 Xamarin.Forms ListView 小部件的 ItemsSource。但是在运行它时,我没有看到列表视图项已填充。该列表视图没有任何内容可显示。我在这里做错了什么?请帮忙。

我猜页面类后面代码的构造函数是在列表视图项目的数据呈现之后绑定的。但我不确定

代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Mvvm2.Mvvm2Page"
             x:Name="page">
  <ContentPage.Padding>
    <OnPlatform x:TypeArguments="Thickness" iOS="0,20,0,0" />
  </ContentPage.Padding>


  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"></ColumnDefinition>
      <ColumnDefinition Width="0"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>



    <ListView x:Name="listViewPeople" HasUnevenRows="True" Grid.Column="0" Grid.Row="0" Header="People List" 
              ItemsSource="{Binding Source={x:Reference page}, Path=People}">

      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ContentView Padding="5">
              <Frame OutlineColor="Accent" Padding="10">
                <StackLayout Orientation="Horizontal">
                  <StackLayout>
                    <Label Text="{Binding Id}"></Label>
                    <Label Text="{Binding FirstName}"></Label>
                    <Label Text="{Binding LastName}"></Label>
                    <Label Text="{Binding Age}"></Label>
                  </StackLayout>
                </StackLayout>
              </Frame>
            </ContentView>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </Grid>
</ContentPage>


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace Mvvm2
{
    public partial class Mvvm2Page : ContentPage
    {
        public static List<Person> People; // public static property

        public Mvvm2Page()
        {
            InitializeComponent();

            // fill property "People"
            People = new List<Person>();
            People = getPeople();
        }


        private List<Person> getPeople()
        {
            var list = new List<Person>();

            var p1 = new Person { Id = 1, Age = 19, FirstName = "Anna", LastName = "Larson" };
            var p2 = new Person { Id = 2, Age = 23, FirstName = "Beri", LastName = "Slovik" };
            var p3 = new Person { Id = 3, Age = 65, FirstName = "Ron", LastName = "Prelosi" };
            var p4 = new Person { Id = 4, Age = 32, FirstName = "William", LastName = "Maxel" };
            var p5 = new Person { Id = 5, Age = 71, FirstName = "Fred", LastName = "Lipez" };
            var p6 = new Person { Id = 6, Age = 44, FirstName = "Dave", LastName = "Vanoviz" };


                    list.Add(p1);
                    list.Add(p2);
                    list.Add(p3);
                    list.Add(p4);
                    list.Add(p5);
                    list.Add(p6);

            return list;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mvvm2
{
    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }


}

【问题讨论】:

    标签: wpf xaml listview xamarin.forms


    【解决方案1】:

    表达式

    public static List<Person> People; // public static property
    

    不声明属性,而是声明字段。

    你需要的是一个非静态的公共属性:

    public static List<Person> People { get; set; }
    

    如果属性的值以后可能发生变化,你应该创建一个视图模型类来声明该属性,并实现INotifyPropertyChanged接口:

    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private List<Person> people;
        public List<Person> People
        {
            get { return people; }
            set
            {
                people = value;
                OnNotifyPropertyChanged(nameof(People));
            }
        }
    
        private void OnNotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    您可以将此类的一个实例分配给您页面的 DataContext

    InitializeComponent();
    var viewModel = new ViewModel();
    viewModel.People = getPeople();
    DataContext = viewModel;
    

    并将 XAML 中的绑定编写为

    ItemsSource="{Binding People}" 
    

    如果您稍后想要设置视图模型的 People 属性,您可以简单地通过 DataContext 属性访问它,例如

    ((ViewModel)DataContext).People = getPeople();
    

    或者您只是将 ViewModel 实例存储在类成员中。

    【讨论】:

    • 感谢您的回复。我会试试你的解决方案。
    【解决方案2】:

    请尝试使用 ObservableCollection

    定义这样的属性

    public ObservableCollection<People> Peoples { get; set; }
    

    然后在你的构造函数中

    Peoples=new ObservableCollection<People>();
    
     var p1 = new Person { Id = 1, Age = 19, FirstName = "Anna", LastName = "Larson" };
                var p2 = new Person { Id = 2, Age = 23, FirstName = "Beri", LastName = "Slovik" };
                var p3 = new Person { Id = 3, Age = 65, FirstName = "Ron", LastName = "Prelosi" };
                var p4 = new Person { Id = 4, Age = 32, FirstName = "William", LastName = "Maxel" };
                var p5 = new Person { Id = 5, Age = 71, FirstName = "Fred", LastName = "Lipez" };
                var p6 = new Person { Id = 6, Age = 44, FirstName = "Dave", LastName = "Vanoviz" };
    
    
    
    
                        Peoples.Add(p1);
                        Peoples.Add(p2);
                        Peoples.Add(p3);
                        Peoples.Add(p4);
                        Peoples.Add(p5);
                        Peoples.Add(p6);
    

    【讨论】:

      猜你喜欢
      • 2020-02-19
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多