2016-04-29 30 views
0

我嘗試在xaml頁面後面的代碼中使用名爲「People」的公共靜態屬性,並將其填充到頁面後面的代碼構造函數中。然後在xaml頁面中,我將該公共屬性綁定到Xamarin.Forms ListView小部件的ItemsSource。但在運行它時,我沒有看到列表視圖項被填充。該列表視圖沒有任何顯示。我在這裏做錯了什麼?請幫忙。將xaml頁面後面的代碼屬性綁定到列表視圖

我猜的頁面類後面的代碼的構造函數綁定後的ListView的項目的數據呈現。但我不知道

代碼:

<?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; } 
    } 


} 

回答

3

表達

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}" 

綁定如果以後要設置視圖模型的人民屬性,您可以簡單地通過DataContext屬性訪問它像

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

,或者你只是將ViewModel實例存儲在類成員中。

+0

謝謝您的答覆。我會嘗試你的解決方案。 –

-1

請的ObservableCollection

嘗試defind屬性這樣

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

然後在constructory

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); 
+0

請問爲什麼downvote – suulisin

相關問題