I want to create a list of users, but I am not able to perform the binding, the data is not displayed. If the Here is my structure:
UserList.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="E_Tools.View.ListUsuario">
<StackLayout Orientation="Vertical" Padding="20">
<ListView x:Name="lstView" ItemsSource="{Binding Usuarios}" BackgroundColor="White">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell
Text="{Binding Nome}"
Detail="{Binding Apelido}"
/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
UserList.cs:
using E_Tools.ViewModel;
using Xamarin.Forms;
namespace E_Tools.View
{
//[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ListUsuario : ContentPage
{
public ListUsuario()
{
InitializeComponent();
ToolbarItem itemUsuario = new ToolbarItem
{
Text = "Novo",
Order = ToolbarItemOrder.Primary,
Command = new Command(() => Navigation.PushAsync(new Usuario()))
};
ToolbarItems.Add(itemUsuario);
BindingContext = new ListagemUsuarioViewModel();
}
}
}
User ListingViewModel.cs:
using E_Tools.Model;
using E_Tools.Repository;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Xamarin.Forms;
namespace E_Tools.ViewModel
{
public class ListagemUsuarioViewModel : INotifyPropertyChanged
{
ObservableCollection<Usuario> Usuarios { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public ListagemUsuarioViewModel()
{
ICollection<Usuario> lstUsuarios = new UsuarioRepository<Usuario>().GetAll();
Usuarios = new ObservableCollection<Usuario>();
foreach (var item in lstUsuarios)
{
this.Usuarios.Add(item);
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}