Repeat StackLayouts according to Xamarin Forms list

1

I created a StackLayout with a labels inside that I need to display some information in them. With this, I need to load StackLayouts and fill the labels according to the amount of records in the list.

Example : The customer can have more than 1 registered address. This view needs to return me the registered addresses of this client. Therefore, it will load StackLayouts with the information filled in according to the number of registered addresses.

How can I do this?

My code:

\\ XAML

 <StackLayout x:Name="stkEnderecos" BackgroundColor="White">

    <Label x:Name="lblNomeDestinatario" Text=" {Binding EndDestinatario}" Font="16" TextColor="Black"></Label>
    <Label x:Name="lblTipoEndereco" Text=" {Binding EndTipo}" Font="16" TextColor="Black"></Label>
    <Label Text="_____________________________________________________" TextColor="Black"></Label>

    <Label x:Name="lblEndereco1" Font="14" TextColor="Black"></Label>
    <Label x:Name="lblEndereco2" Font="14" TextColor="Black"></Label>
    <Label x:Name="lblEndereco3" Font="14" TextColor="Black"></Label>

  </StackLayout>

    </StackLayout>

\\ .CS

 public MeusEnderecosView()
        {
            InitializeComponent();

            objAcesso = new AcessaDados();
            List<ClienteDB> ListCliente = objAcesso.Listar();

            if (ListCliente.Count > 0)
            {
                objCliente = new Clientes();
                objCliente.CliCPF = ListCliente[0].CPF;
                objCliente.CliSenha = ListCliente[0].Senha;
            }

            var viewModels = new ViewModels.MeusEnderecosViewModel(this, Api.APICliente.ConsultarCliente(objCliente));

            this.lblEndereco1.Text = " " + viewModels.EndLogradouro + ", " + viewModels.EndNumero + ", " + viewModels.EndComplemento;
            this.lblEndereco2.Text = " " + viewModels.EndBairro + ", " + viewModels.EndCidade + ", " + viewModels.EndUF;
            this.lblEndereco3.Text = " CEP: " + viewModels.EndCep;

            this.BindingContext = viewModels;
        }

\\ ViewModel

  public MeusEnderecosViewModel(ContentPage Page, Clientes objCliente)
            {
                this.page = Page;
                objCliEnd = new ClientesEndereco();
                var picTpEndereco = page.FindByName<Picker>("picTpEndereco");
                var picUF = page.FindByName<Picker>("picUF");
                var stkEnderecos = page.FindByName<StackLayout>("stkEnderecos");

                objCliEnd.CliEndCodigo = objCliente.CliCodigo;

                List<ClientesEndereco> objListClientesEnderecos = new List<ClientesEndereco>();

                objListClientesEnderecos = Api.APICliente.ListarEnderecos(objCliEnd.CliEndCodigo); 
\ CHAMADA DA API QUE RETORNA A LISTA DE ENDEREÇOS CADASTRADOS DO BD DE ACORDO COM O CÓDIGO DO CLIENTE

                foreach (ClientesEndereco objEndereco in objListClientesEnderecos)
                {
                    objCliEnd = new ClientesEndereco();
                    objCliEnd.EndDestinatario = objEndereco.EndDestinatario;
                    objCliEnd.EndTipo = objEndereco.EndTipo;
                    objCliEnd.EndNumero = objEndereco.EndNumero;
                    objCliEnd.EndComplemento = objEndereco.EndComplemento;
                    objCliEnd.EndBairro = objEndereco.EndBairro;
                    objCliEnd.EndCidade = objEndereco.EndCidade;
                    objCliEnd.EndUF = objEndereco.EndUF;
                    objCliEnd.EndCep = objEndereco.EndCep;
                }
            }
    
asked by anonymous 22.12.2016 / 16:25

1 answer

2

Working with ListView

<ContentPage.BindingContext>
    <viewModels:MeusEnderecosViewModel/>
  </ContentPage.BindingContext>

    <ListView ItemsSource="{Binding objListClientesEnderecos}">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <StackLayout>
              ...
            </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
    
22.12.2016 / 19:27