How to set ItemsSource of ListView?

我与影子孤独终老i 提交于 2021-01-27 04:20:29

问题


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

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: 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");
    }
}

How can I set the ItemsSource of my XAML?

Edit:

Now I tried the suggestion from user2425632 and it works if I do the following changes:

  1. Adding xmlns:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld" to my XAML file

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.

<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

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

回答1:


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. 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

Example cs code

Example xaml code

Hope that helps.




回答2:


I think I have the solution for your problem. I had the same issue and I added this line in 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




回答3:


You can use ObservableCollections. This type of collection, notifies when changes are made to it.

In your code:

.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:

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

And then, add the necessary method to NotifyOnPropertyChanged:

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

In your .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.

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


来源:https://stackoverflow.com/questions/28738090/how-to-set-itemssource-of-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!