如何设置ListView的ItemsSource?

How to set ItemsSource of ListView?

这里我定义了我的数据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:

<ListView x:Name="listView"
                IsVisible="false"
                ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"
                ItemSelected="EmployeeListOnItemSelected">

这应该有效吗?因为我得到

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

编辑:

现在我尝试了 user2425632 的建议,如果我进行以下更改,它会起作用:

  1. 正在将 xmlns:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld" 添加到我的 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:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"
             x:Class="HelloXamarinFormsWorld.EmployeeListPage"
             Title="Employee List">
    <ContentPage.Content>

当然,您必须更改名称以适合您的项目。

  1. 显示列表视图

我删除了 IsVisibleItemSelected

<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
  1. 使一切静态化

它必须是静态的,否则你会得到

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 ());
}

您可以使用 ObservableCollections。这种类型的集合会在对其进行更改时发出通知。

在您的代码中:

.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>();
        ....
    }

您必须在默认构造函数中添加一个DataContext = this

public YourClass()
{
    InitializeComponent();
    DataContext = this;
}

然后,将必要的方法添加到NotifyOnPropertyChanged:

 protected virtual void OnPropertyChanged(string propertyName)
 {
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
 }

在你的.xaml:

<ListView x:Name="listView"
    IsVisible="false"
    ItemsSource="{Binding Path=ObservableEmployees, ElementName=EmployeesWindow, NotifyOnSourceUpdated=True}"
    ItemSelected="EmployeeListOnItemSelected">

ElementName=EmployeesWindow中,EmployeesWindow是你的主要Windowx:Name

<Window ...
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                      
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="EmployeesWindow" >

所以我自己并没有真正开始这样做,但是通过阅读文档我有一个建议可能值得你尝试。

ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"

在您的 xaml 中,您说过源代码是静态的,但查看您的 .cs 文件却不是。尝试以下操作:

public static List<Employee> myListOfEmployeeObjects { private set; get; }

然后尝试使用静态函数设置对象,例如:

static App() {
    myListOfEmployeeObjects = something;
}

然后该列表应该可以在页面上查看。

我使用了以下您可能会觉得有用的链接:

Xamarin documentation on data-binding

Example cs code

Example xaml code

希望对您有所帮助。

我想我可以解决您的问题。我遇到了同样的问题,我在 EmployeeListPage.xaml 中添加了这一行:

xmlns:local="clr-namespace:YourNamespace;assembly=YourAssembly"

您将在项目的属性中获得程序集的名称,在任何 page.cs

中获得命名空间