Repeat Stacklayouts according to Xamarin Forms list

Asked

Viewed 93 times

1

I created a StackLayout with a labels inside that I need to display some information on them. With that, I need it charged StackLayouts and completed the labels according to the amount of records on the list.

Example: The client may have more than 1 registered address. This view needs to return me the registered addresses of this client. So, load StackLayouts with the information completed according to the number of registered addresses.

How can I do that ?

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;
                }
            }

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.