我正在尝试从数据填充 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"
x:Class="FormDemos.Views.Feedback">
<ContentPage.Content>
<Label>I am Testimonials Page</Label>
<ListView x:Name="FeedbackList" />
</ContentPage.Content>
</ContentPage>
在CS文件中
namespace FormDemos.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Feedback : ContentPage
{
class City
{
public string Name
{
get;
set;
}
public string State
{
get;
set;
}
}
public Feedback()
{
InitializeComponent();
var cities = new List<City>() {
new City() {State = "FL", Name = "Miami"},
new City() {State = "CA", Name = "San Francisco"},
new City() {State = "CA", Name = "Los Angeles"},
new City() {State = "FL", Name = "Orlando"},
new City() {State = "TX", Name = "Houston"},
new City() {State = "NY", Name = "New York City"},
};
FeedbackList.ItemsSource = cities;
}
}
}
每次我构建这个项目时,我都会收到错误“序列不包含任何元素”。我看到一些在线教程具有相同的代码并且运行良好。这里出了什么问题?
最佳答案
你必须在布局中包含控件
<?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="FormDemos.Views.Feedback">
<ContentPage.Content>
<StackLayout>
<Label>I am Testimonials Page</Label>
<ListView x:Name="FeedbackList" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
我写了这个little article关于这个……
关于c# - 序列不包含 ListView 的 xamarin 表单中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42717656/