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));
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.
- List view display
I removed IsVisible and ItemSelected .
<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
- 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 ()); }