[英]How to set ItemsSource of ListView?
Here I have defined my data myListOfEmployeeObjects : 这里我定义了我的数据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 ());
}
}
Than I have my XAML where I set the ItemsSource
: 比我设置
ItemsSource
XAML:
<ListView x:Name="listView"
IsVisible="false"
ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"
ItemSelected="EmployeeListOnItemSelected">
Should this work? 这有用吗? Because I get
因为我明白了
Xamarin.Forms.Xaml.XamlParseException: Type App not found in xmlns
Xamarin.Forms.Xaml.XamlParseException:在xmlns中找不到类型App
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");
}
}
How can I set the ItemsSource
of my XAML? 如何设置XAML的
ItemsSource
?
Edit: 编辑:
Now I tried the suggestion from user2425632 and it works if I do the following changes: 现在我尝试了user2425632的建议,如果我做了以下更改,它会起作用:
xmlns:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"
to my XAML file xmlns:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"
到我的XAML文件 It now looks like the following 它现在看起来如下
<?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. 当然,您必须更改名称,以使其适合您的项目。
I removed the IsVisible
and the ItemSelected
. 我删除了
IsVisible
和ItemSelected
。
<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
It has to be static, otherwise you get 它必须是静态的,否则你会得到
No static member found for local:App.myListOfEmployeeObjects
找不到本地的静态成员: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}"
In your xaml you've said that the source is static but looking at your .cs file it isn't. 在您的xaml中,您已经说过源是静态的,但是查看您的.cs文件却不是。 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: 我使用了以下您可能会觉得有用的链接:
Xamarin documentation on data-binding 关于数据绑定的Xamarin文档
Hope that helps. 希望有所帮助。
I think I have the solution for your problem. 我想我有解决你的问题的方法。 I had the same issue and I added this line in EmployeeListPage.xaml :
我有同样的问题,我在EmployeeListPage.xaml中添加了这一行:
xmlns:local="clr-namespace:YourNamespace;assembly=YourAssembly"
You'll get the name of assembly in the properties of your project and the namespace in any page.cs 您将在项目的属性和任何page.cs中的命名空间中获得程序集的名称
You can use ObservableCollections
. 您可以使用
ObservableCollections
。 This type of collection, notifies when changes are made to it. 此类集合会在对其进行更改时通知。
In your code: 在你的代码中:
.cs: 的.cs:
public class YourClass : INotifyPropertyChanged //Note this INotifyPropertyChanged
{
private ObservableCollection<Employee> _myListOfEmployeeObjects;
public ObservableCollection<Employee> ObservableEmployees
{
get
{
if (_myListOfEmployeeObjects == null) LoadEmployees();
return _myListOfEmployeeObjects;
}
set
{
_myListOfEmployeeObjects = value;
OnPropertyChanged("ObservableEmployees");
}
}
private void LoadEmployees()
{
// Necessary stuff to load employees
ObservableEmployees = new ObservableCollection<Employees>();
....
}
You must add a DataContext = this
in your default constructor: 您必须在默认构造函数中添加
DataContext = this
:
public YourClass()
{
InitializeComponent();
DataContext = this;
}
And then, add the necessary method to NotifyOnPropertyChanged
: 然后,将必要的方法添加到
NotifyOnPropertyChanged
:
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
In your .xaml: 在你的.xaml中:
<ListView x:Name="listView"
IsVisible="false"
ItemsSource="{Binding Path=ObservableEmployees, ElementName=EmployeesWindow, NotifyOnSourceUpdated=True}"
ItemSelected="EmployeeListOnItemSelected">
In ElementName=EmployeesWindow
, the EmployeesWindow
is your main Window x:Name
. 在
ElementName=EmployeesWindow
, EmployeesWindow
是您的主窗口x:Name
。
<Window ...
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="EmployeesWindow" >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.