如何设置ListView的ItemsSource?

7

这里我定义了我的数据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:在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");
    }
}

我该如何设置我的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>

当然,您需要更改名称以适应您的项目。
2. 显示列表视图
我删除了IsVisibleItemSelected
<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
  1. 将所有东西都设为静态的

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

找不到本地变量 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 ());
}
3个回答

3

我自己还没有做过这件事情,但是从文档中了解到的建议是值得你尝试的。

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

在你的XAML中,你说源是静态的,但是看你的.cs文件,它并不是静态的。请尝试以下操作:
public static List<Employee> myListOfEmployeeObjects { private set; get; }

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

static App() {
    myListOfEmployeeObjects = something;
}

然后列表应该在页面上可见。
我使用了以下链接,您可能会发现有用: Xamarin数据绑定文档 示例cs代码 示例xaml代码 希望这有所帮助。

1

我认为我有解决您问题的方法。我曾经遇到过同样的问题,然后我在EmployeeListPage.xaml中添加了这一行:

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

您可以在项目属性中获取程序集名称,在任何page.cs文件中获取命名空间。


0
你可以使用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 是你的主窗口 x:Name
<Window ...
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                      
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="EmployeesWindow" >

谢谢您的回复。我会尝试一下。我能否像之前那样使用普通列表呢?或者只能使用ObservableCollections吗?在这里,只使用了静态的IEnumerable...(链接) - testing
我认为绑定只能在ObservableCollections中看到。尝试这段代码,如果它可以工作,再尝试将其更改为您拥有的常规列表。它们并不难。就像列表一样 :) - Sonhja
现在我尝试了你的代码(见这里)。有些地方对我来说不太清楚。1. InitializeComponent()是什么?2.DataContext是什么?我将它们注释掉了,否则会出现构建错误。public event PropertyChangedEventHandler PropertyChanged;缺失,我添加了它。3.我的主窗口是什么?我的主窗口不在XAML中。也许你是在谈论WPF,因为Xamarin版本看起来不同。我从.xaml文件中将其删除。当我尝试运行代码时,我得到了“对象引用未设置为对象实例”的错误。 - testing
InitializeComponent() 方法是在创建 WPF 项目时自动生成的,因此您无需修改或编写它。只需查找它并在其后添加 DataContext 行即可。 DataContext 告诉应用程序,任何数据更改都将在同一上下文中共享。 无论如何...您能否发布完整的代码?请添加 .xaml.cs - Sonhja
我认为WPF与Xamarin方法不同。DataContext = BindingContext?尽管如此,.cs.xaml。很抱歉,我不能使用其他许多服务,因为这里的网络过滤器会阻止一切... - testing

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接