简体   繁体   English

如何设置ListView的ItemsSource?

[英]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的建议,如果我做了以下更改,它会起作用:

  1. Adding 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. 当然,您必须更改名称,以使其适合您的项目。

  1. Showing list view 显示列表视图

I removed the IsVisible and the ItemSelected . 我删除了IsVisibleItemSelected

<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
  1. Make everything static 让一切都静止

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文档

Example cs code 示例cs代码

Example xaml code 示例xaml代码

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=EmployeesWindowEmployeesWindow是您的主窗口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.

相关问题 如何在 Xamarin 中将列表视图项源设置为视图模型? - How to set listview itemssource to a viewmodel in Xamarin? 如何设置ColumnSeries的ItemsSource值? - How to set ItemsSource value of ColumnSeries? 如何以编程方式设置ItemsSource属性? - How to set ItemsSource property programmatically? 在ListView中设置SelectedItem之前如何加载ItemsSource? - How to load ItemsSource before setting SelectedItem in ListView? 设置从ItemsSource到ListView DataTemplate中没有XAML的Label的绑定 - Set binding from ItemsSource to Label in ListView DataTemplate without XAML 将ItemsSource设置为列表时,使“ IsMouseOver”在ListView上工作 - Getting 'IsMouseOver' to work on a ListView when ItemsSource is set to a list 当使用MVVM将事件绑定到ListView ItemsSource生成的元素时,如何修复“对象引用未设置为对象实例。” - How to fix “Object reference not set to an instance of object.” when binding an event to an element generated by ListView ItemsSource when using MVVM? 如何从Observablecollection设置组合框的ItemsSource - How to set ItemsSource of a combobox from a Observablecollection 使用ItemsSource时如何设置TabItem CommandBindings - How to set TabItem CommandBindings when using ItemsSource 如何在XAML中设置DataGrid的ItemsSource? - How do I set the ItemsSource of a DataGrid in XAML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM