Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Binding com ListView

Estou com problema no uso do binding com listview. No meo codigo nada aparece na lista. Correçao?

<?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="AluraNutricao.ListarRefeicoes"
             Title="Lista de refeições">
  <!--<Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />-->

  <StackLayout Padding="25">
    <!--<Label Text="Listagem das refeições"/>-->
    <Label Text="{Binding Nome}" />

    <ListView ItemsSource="{Binding refeicoes}" ItemTapped="removeItem">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <StackLayout>
                <Label Text="{Binding  descricao}" FontSize="12" />
                <StackLayout Orientation="Horizontal">
                  <Label Text="Calorias:" FontSize="10" />
                  <Label Text="{Binding calorias}" FontSize="10" />
                </StackLayout>
              </StackLayout>
            </ViewCell.View>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </StackLayout>
</ContentPage>

... using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks;

using Xamarin.Forms;

namespace AluraNutricao { public partial class ListarRefeicoes : ContentPage { //public ListarRefeicoes() //{ // Nome = "Alura"; // BindingContext = this; // refeicoes = new List(); // InitializeComponent(); //}

public ListarRefeicoes(ObservableCollection refeicoes) { Nome = "Alura"; //BindingContext = this; //refeicoes = new List(); this.refeicoes = refeicoes; BindingContext = this; InitializeComponent(); }

public async void removeItem(object sender, ItemTappedEventArgs e) { Refeicao refeicao = e.Item as Refeicao; var resposta = await DisplayAlert("Remover item", "Você tem certeza que deseja remover a refeição " + refeicao.descricao, "Sim", "Não");

if (resposta) { refeicoes.Remove(refeicao); await DisplayAlert("Remover item", "Refeicao removida com sucesso", "Ok"); }

refeicoes.Remove(refeicao); }

public string Nome { get; private set; } ObservableCollection refeicoes; // private List refeicoes; } } ...

1 resposta
solução!

Resolvido. Na verdade falta os getters e setters.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software