How to set ItemsSource in ListView?

Here I 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 XAML where I set 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 Application 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 my XAML?

Edit:

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

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

Now it looks like this

 <?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 must change the names to fit your project.

  1. List view display

I removed IsVisible and ItemSelected .

 <ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"> 
  1. Make everything static

It must be static, otherwise you will 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 ()); } 
+6
source share
3 answers

So I'm not really going to do it myself, but, while reading the documentation, I have a suggestion that might cost you a try.

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

In your xaml, you said that the source is static, but looking at your .cs file is not. Try the following:

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

and then try setting the object using a static function, for example:

 static App() { myListOfEmployeeObjects = something; } 

Then the list should be available for viewing on the page.

I used the following links, which may be useful:

Xamarin Data Binding Documentation

Cs code example

Xaml code example

Hope this helps.

+3
source

I think I have a solution to the problem. I had the same problem and I added this line to EmployeeListPage.xaml:

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

You will get the assembly name in the properties of your project and namespace on any page.cs

+1
source

You can use ObservableCollections . This type of collection notifies you of changes 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 should add DataContext = this to your default constructor:

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

Then add the required 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 EmployeesWindow is your main x:Name window.

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

Source: https://habr.com/ru/post/983031/


All Articles