xamarin - 从列表中填充列表视图
问题描述
在 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>
我检查了列表“全部”是否已填充,但我不知道应该做什么才能显示。
解决方案
上面的代码中有两个All
。
一个是
public static IList<PostList> All { set; get; }
,
另一个在你的method()
List<PostList> All = new List<PostList>();
尝试更改它以使它们保持一致:
public void method()
{
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);
}
}
我也对你如何实例化你的 ViewModel 感到困惑。你在哪里用这个public PostList(List<Post> list)
构造函数传递参数?
我们通常这样做:
<ListView SeparatorVisibility="Default"
x:Name ="listview">
...
</ListView>
在后面的代码中:
public YourPage()
{
InitializeComponent();
PostList list = new PostList(your List<Postlist>);
listview.ItemsSource = list.All;
}
视图模型:
public class 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()
{
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 IList<PostList> All { set; get; }
}
或使用x:Static
资源:
public class PostList
{
public String Body { get; set; }
public String Subject { get; set; }
public Coordinate Position { get; set; }
private static List<Post> all = new List<Post>();
static PostList()
{
all = xxx;//here create a List<Post>
method();
}
public static void method()
{
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 IList<PostList> All { set; get; }
}
然后你可以在你的 xaml 中绑定,比如:
<ListView SeparatorVisibility="Default"
ItemsSource="{x:Static local:PostList.All}">
...
</ListView>
推荐阅读
- python - 当我有训练集、开发集和测试集时,我是否将缩放器安装在 X 或 X_train 上?
- angular - 如何在 ngModel 上发布数组?
- python - csv 和 pandas 的更快替代品
- eclipse-plugin - 在 Eclipse ADT 选项卡描述中显示系统+客户端?
- elasticsearch - 删除elasticsearch中的所有索引
- python - 如何在 Django 应用程序模型中定义外键,如果未安装子应用程序将被忽略
- python - 如何通过标识整数转换数据框列的每个值
- apache - Apache 尝试完整的文件名(GET 参数作为文件名的一部分)
- java - 使用 Mockito 模拟测试类的数据库调用
- java - 如何使用java在html中附加标签