Quero criar uma listagem de usuários, porém, não estou conseguindo realizar o binding, os dados não são exibidos. Segue minha estrutura:
ListUsuarios.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>
ListUsuarios.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();
}
}
}
ListagemUsuarioViewModel.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));
}
}
}