xamlページの背後にあるコードで "People"という静的な静的プロパティを使用しようとしました。コードビハインドページのコンストラクタで埋められます。次にxamlページで、パブリックプロパティをXamarin.Forms ListViewウィジェットのItemsSourceにバインドさせました。しかし、それを実行すると、私はリストビューの項目が満たされて表示されません。そのリストビューには何も表示されません。私はここで間違って何をしていますか?助けてください。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"
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; }
}
}
は、ご返信いただきありがとうございます。私はあなたの解決策を試みます。 –