0
I want to create a list of users, however, I am not able to perform Binding, the data is not displayed. If the Follows my structure:
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();
}
}
}
List of users viewmodel.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));
}
}
}