How to set ItemsSource of ListView?
Solution 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
Hope that helps.
Solution 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
testing
Updated on June 12, 2022Comments
-
testing almost 2 years
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:
- 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.
- Showing list view
I removed the
IsVisible
and theItemSelected
.<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
- 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 ()); }
- Adding
-
testing about 9 yearsThanks for your response. I'll try it out. Would it be possible to use a normal list as I did? Or can this only be done with
ObservableCollections
? Here only a staticIEnumerable
has been used ... -
Sonhja about 9 yearsI think bindings will be only seen with
ObservableCollections
. Try this code, and if it works, try to change it with a regular list as you have. They are not difficult. The same like lists :) -
testing about 9 yearsNow I tried your code (see here). Some things are not clear for me. 1. What is
InitializeComponent()
? 2. What isDataContext
? I commented them out because otherwise I get an build error.public event PropertyChangedEventHandler PropertyChanged;
was missing and I added it. 3. What is my main window? My main window is not in XAML. Perhaps are you talking from WPF, because the Xamarin version does look different. I removed it from the .xaml. When I try running the code I get Object reference not set to an instance of an object. -
Sonhja about 9 yearsThe
InitializeComponent()
is auto-generated when you first create an WPF project, so you don't have to modify or write it. Just look for it and add theDataContext
line after. TheDataContext
tells the application that, any data change, will be shared in the same context. Anyways... can you post full code please? Add.xaml
and.cs
. -
testing about 9 years