【发布时间】:2021-03-08 20:04:09
【问题描述】:
在 Xamarin 文档https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/boxview 中给出的示例“使用 BoxView 列出颜色”的启发下,我一直在尝试以相同的方式填充我的列表视图。但是什么都不会出现。
在我的类 PostList 中,它创建一个对象看起来像这样:
public String Body { get; set; }
public String Subject { get; set; }
public Coordinate Position { get; set; }
private List<Post> all = new List<Post>();
public PostList(List<Post> list)
{
all = list;
method();
}
public void method()
{
List<PostList> All = new List<PostList>();
foreach(Post x in all)
{
PostList Pl = new PostList
{
Subject = x.getSubject(),
Body = x.getBody(),
Position = x.getCoordinate()
};
All.Add(Pl);
}
}
public static IList<PostList> All { set; get; }
下面给出了用于显示的 XAML 类:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:EE.Model"
x:Class="EE.View.CWT"
NavigationPage.HasNavigationBar="False"
NavigationPage.BackButtonTitle="False"
ControlTemplate="{StaticResource Template}">
<ListView SeparatorVisibility="Default"
ItemsSource="{x:Static local:PostList.All}">
<ListView.RowHeight>
<OnPlatform x:TypeArguments="x:Int32">
<On Platform="iOS, Android" Value="180" />
</OnPlatform>
</ListView.RowHeight>
<ListView.WidthRequest Value="20"/>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Margin="5,5,5,5">
<Frame OutlineColor="LightGray">
<StackLayout>
<StackLayout Orientation="Horizontal">
<StackLayout x:Name="SL" Padding="0">
<Label x:Name="Title" Text="{Binding Subject}"/>
</StackLayout>
</StackLayout>
</StackLayout>
</Frame>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
我已经检查了“全部”列表是否已填充,但我不知道应该做什么才能显示。
【问题讨论】:
-
PostList是您的页面或模型背后的代码吗?如果你想创建一个简单的 ListView 有更好的例子可以遵循,比如docs.microsoft.com/en-us/xamarin/get-started/tutorials/listview/… -
如果您不必使用
INotifyPropertyChanged,请使用ObservableCollection<T>
标签: xamarin xamarin.forms data-binding itemsource