如何设置ListView的ItemsSource? [英] How to set ItemsSource of ListView?
问题描述
这里我定义了我的数据myListOfEmployeeObjects:
Here I have defined my data myListOfEmployeeObjects:
public class App : Application
{
public List<Employee> myListOfEmployeeObjects;
public App ()
{
Employee emp1 = new Employee () {
FirstName = "Max",
LastName = "Mustermann",
Twitter = "@fake1"
};
Employee emp2 = new Employee () {
FirstName = "Evy",
LastName = "Mustermann",
Twitter = "@fake2"
};
myListOfEmployeeObjects = new List<Employee> {
emp1, emp2
};
MainPage = new NavigationPage (new EmployeeListPage ());
}
}
比我在 XAML 中设置 ItemsSource
的地方:
Than I have my XAML where I set the ItemsSource
:
<ListView x:Name="listView"
IsVisible="false"
ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"
ItemSelected="EmployeeListOnItemSelected">
这应该有效吗?因为我得到
Should this work? Because I get
Xamarin.Forms.Xaml.XamlParseException:在 xmlns 中找不到类型应用
Xamarin.Forms.Xaml.XamlParseException: Type App not found in xmlns
public partial class EmployeeListPage : ContentPage {
private ListView listView;
private void InitializeComponent() {
this.LoadFromXaml(typeof(EmployeeListPage)); // here the exception is thrown
listView = this.FindByName <ListView>("listView");
}
}
如何设置 XAML 的 ItemsSource
?
How can I set the ItemsSource
of my XAML?
现在我尝试了来自 user2425632 的建议,如果我进行以下更改,它会起作用:
Now I tried the suggestion from user2425632 and it works if I do the following changes:
- 将
xmlns:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"
添加到我的 XAML 文件
- Adding
xmlns:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"
to my XAML file
现在看起来像下图
<?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:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"
x:Class="HelloXamarinFormsWorld.EmployeeListPage"
Title="Employee List">
<ContentPage.Content>
当然,您必须更改名称以适合您的项目.
Of course you have to change the names so that it suits to your project.
- 显示列表视图
我删除了 IsVisible
和 ItemSelected
.
<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
- 让一切保持静态
它必须是静态的,否则你会得到
It has to be static, otherwise you get
未找到 local:App.myListOfEmployeeObjects 的静态成员
No static member found for local:App.myListOfEmployeeObjects
public static List<Employee> myListOfEmployeeObjects { private set; get; }
public static void GetAllEmployees(){
Employee emp1 = new Employee () {
FirstName = "Max",
LastName = "Mustermann",
Twitter = "@fake1"
};
Employee emp2 = new Employee () {
FirstName = "Eva",
LastName = "Mustermann",
Twitter = "@fake2"
};
myListOfEmployeeObjects = new List<Employee> {
emp1, emp2
};
}
public App ()
{
GetAllEmployees ();
MainPage = new NavigationPage (new EmployeeListPage ());
}
推荐答案
所以我自己实际上还没有开始做这件事,但是通过阅读文档我有一个建议,可能值得你尝试.
So I haven't actually gotten around to doing this myself but from reading the documentation I have a suggestion which may be worth you trying.
ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"
在您的 xaml 中,您已经说过源代码是静态的,但查看您的 .cs 文件却不是.请尝试以下操作:
In your xaml you've said that the source is static but looking at your .cs file it isn't. Try the following:
public static List<Employee> myListOfEmployeeObjects { private set; get; }
然后尝试使用静态函数设置对象,例如:
and then try and set the object using a static function, eg.:
static App() {
myListOfEmployeeObjects = something;
}
然后列表应该可以在页面上查看.
Then the list should be viewable on the page.
我使用了您可能会觉得有用的以下链接:
I used the following links which you may find useful:
希望有所帮助.
这篇关于如何设置ListView的ItemsSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!