Home > database >  Retrive label value within a ListView in a MAUI XAML page
Retrive label value within a ListView in a MAUI XAML page

Time:09-09

I have a ListView that gets populated from a REST API. I need to send a parameter to another page to populate another ListView with REST API according to the query.

The list view structure goes like this:

<ListView x:Name="Clientes_List" 
              RowHeight="60"
              HeightRequest="600"
              ItemTapped="TapCliente">
        
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Vertical" Padding="8,0,8,0">
                        <Label Text="{Binding Marca}" TextColor="#45678e" FontSize="14" LineBreakMode="TailTruncation"></Label>
                        <Label Text="{Binding ID}" TextColor="#FF0000" FontSize="12" x:Name="LtIdCliente"></Label>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

The parameter that needs to be passed is the one declared in the text value in the LtIdCliente label.

This is my action:

public void TapCliente(object sender, ItemTappedEventArgs e)
{
    Label lbClienteId = (Label)Clientes_List.FindByName("LtIdCliente");

    string clienteId = lbClienteId.Text;

    Navigation.PushModalAsync(new Facturas(clienteId));

}

I'm getting an objet not set to an instance error, so it's not taking the value from the label.

System.NullReferenceException: 'Object reference not set to an instance of an object.'

The receiver page "Facturas.xaml", will read the parameter in this way:

public partial class Facturas : ContentPage
{
public Facturas(string idCliente)
{
    InitializeComponent();

    if (Connectivity.Current.NetworkAccess == NetworkAccess.None)
    {
        lblInternet.Text = "Tu conexión a internet es deficiente o nula, no podemos realizar consultas.";
    }
    else
    {
        lblInternet.Text = "Conexión estable con el servidor de Haif.";

        CargarData(idCliente);

    }

}

public async void CargarData(string idCliente)
{
    var contenido = "";
    
    HttpClient cliente = new HttpClient();
    var restUrl = "https://*************/api/FacturaCliente?id="   idCliente;
    cliente.BaseAddress = new Uri(restUrl);

    cliente.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage respuesta = await cliente.GetAsync(restUrl);
    contenido = await respuesta.Content.ReadAsStringAsync();
    var items = JsonConvert.DeserializeObject<List<Factura>>(contenido);
    Facturas_List.ItemsSource = items;
}

#region Clase Facturas

public class Factura
{
    public int ID { get; set; }
    public string NumeroFactura { get; set; }
    public string Total { get; set; }
}

#endregion
}

What will be the correct way to pass that paremeter?

CodePudding user response:

I was looking the issue from a wrong perspective.

I already had the data within the class, so all I needed to do was passing it as a parameter:

public void TapCliente(object sender, ItemTappedEventArgs e)
{

    var clte = (Cliente)Clientes_List.SelectedItem;

    Navigation.PushModalAsync(new Facturas(clte.ID.ToString()));

}
  • Related