xamarin - Xamarin 表单列表视图“null”
问题描述
我需要用数据填充简单的 ListView,但 listView 仍然为空,我不知道为什么。代码(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"
xmlns:local="clr-namespace:SecuKey"
x:Class="SecuKey.TransactionViewController">
<ListView x:Name="MainListView"
HasUnevenRows="True" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical">
<Label Text="{Binding Name}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
和 c#:
using System;
using System.Collections.Generic;
using SecuKey.Controllers.ViewControllers;
using Newtonsoft.Json;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace SecuKey
{
public class ListViewTemplate
{
public string Name { get; set; }
}
public partial class TransactionViewController : ContentPage
{
public TransactionViewController()
{
// Just for test
MainListView.ItemsSource = new List<ListViewTemplate> {
new ListViewTemplate
{
Name = "Xamarin.Forms",
},
new ListViewTemplate
{
Name = "Android",
},
new ListViewTemplate
{
Name = "IOS",
},
new ListViewTemplate
{
Name = "Windows",
}
};
}
}
问题在这里: MainListView.ItemsSource = new List {} 因为 MainListView 为空...请提供任何建议?我跟着一个教程,没有更多的东西了..
谢谢
解决方案
public MainPage()
{
InitializeComponent();
List<ListViewTemplate> lists = new List<ListViewTemplate>();
string[] N1 = { "Android", "Ios", "Windows" };
for(int j=0;j< N1.Length; j++)
{
ListViewTemplate listView = new ListViewTemplate();
listView.Name = N1[j];
lists.Add(listView);
}
MainListView.ItemsSource = lists;
}
推荐阅读
- spring-boot - 使用 maven 构建应用程序时出现 ClassNotFoundException
- bash - 使用 Twurl 上传带有变量的图像的问题 - BASH SCRIPT
- ansible - 带有_items的组节点上的ansible循环并避免ssh
- pyspark - 如何在 pyspark 中解压 tar.gz 文件?tar.gz 中有 10 个文件。我有兴趣只阅读 10 个文件中的一个
- python - pivot_table 中的错误:Dropna=False:数组太大;`arr.size * arr.dtype.itemsize` 大于最大可能大小
- javascript - HTML CSS JS,无法打开我制作的实时代码编辑器
- javascript - 如何随机替换特定 URL 的 HREF?innerHTML 影响延迟加载
- pandas - 从日期范围中删除几个日期
- django - 芹菜时间表
- android - 如何拦截Android中的所有onClick事件?