Binding does not work with listview

TuryBazzoTuryBazzo USMember ✭✭
edited December 2016 in Xamarin.Forms

I have a XAML (home.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="AppLuterking.View.Home">

  <Label Text="{Binding titulo}"></Label>

  <StackLayout>
    <ListView ItemsSource="{Binding simulados}">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <StackLayout>
                <Label Text="{Binding description}" />
              </StackLayout>
            </ViewCell.View>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </StackLayout>

</ContentPage>

And home.xaml.cs:

namespace AppLuterking.View
{
    public partial class Home : ContentPage
    {
        public Home(aluno aluno)
        {
            this.BindingContext = new HomeViewModel(aluno, this);

            InitializeComponent();
        }
    }
}

And my HomeViewModel.cs :

namespace AppLuterking.ViewModel
{
    public class HomeViewModel
    {
        public string titulo { get; set; }

        public ObservableCollection<simulado> simulados { get; set; }


        public HomeViewModel(aluno aluno, ContentPage page)
        {
            this.titulo = "Titulo 1";

            page.DisplayAlert("teste", "teste", "OK");

            RetornaSimulados(aluno);
        }

        private async void RetornaSimulados(aluno aluno)
        {
            var cliente = new HttpClient();

            var uri = new Uri($"**********************************************************");

            var response = await cliente.GetAsync(uri);

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                this.simulados = JsonConvert.DeserializeObject<ObservableCollection<simulado>>(content.ToString());
            }

        }
    }
}

I using MVVM but my binding does not work, does anyone know why?

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    What does it means that it does not work? There are items in the list? It's empty?
  • TuryBazzoTuryBazzo USMember ✭✭

    No, i not implement INotifyPropertyChanged.

    Do not load the Binding (titulo,simulados), my view load is empty.

    I tried to use list<> in place of ObservableCollection but not work :(

Sign In or Register to comment.