Xamarin.Forms - Listview only displays if I click on the screen.

Asked

Viewed 136 times

0

Hello,

I got a problem.

Have my listview, where you bring the information as it should, but this information is only displayed when you click on the screen. If it is not clicked on the screen, the information does not appear.

Does anyone have any idea what might be going on?

From now on, thank you.

Design/Layout:

<?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="APPDiretoPonto.View.LinhasOnibus">
    <!--ItemsSource="{Binding Items}"-->    
    <AbsoluteLayout>        
            <ListView x:Name="MyListView"            
                        ItemTapped="Handle_ItemTapped"           
                        CachingStrategy="RecycleElement"
                        HasUnevenRows="True">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding nome}" TextColor="Green" Height="32"/>
                </DataTemplate>
            </ListView.ItemTemplate>
            </ListView>
    </AbsoluteLayout>
</ContentPage>

Source code on the C# of the page in question:

using APPDiretoPonto.Model;
using APPDiretoPonto.WEBService;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace APPDiretoPonto.View
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LinhasOnibus : ContentPage
    {        
        public ObservableCollection<Linha> Items { get; set; }        

        public LinhasOnibus(string sentido)
        {            
            //inicializa
            InitializeComponent();

            //this.IsBusy = false;

            //vai no web service e retorna
            Task.Run(async () => await LoadItems(sentido));

            Task.Delay(1000);

            return;
        }

        async Task LoadItems(string sentido)
        {
            serviceLinha service = new serviceLinha();
            listaLinhas bairro = await service.BuscaOnibus();

            //adiciono o sentidp para buscar a informação e passar como parametro
            foreach (Linha linhas in bairro.linhas) {
                linhas.sentido = sentido;
            }

            //transformo em lista ordernada alfabetica  
            var listaOrdenada = bairro.linhas.OrderBy(x => x.nome).ToList();
            listaOrdenada?.RemoveAll(x => x.nome.Contains("teste")); //removo "teste" que vem do webservice                                    
            //jogo para a list view
            Items = new ObservableCollection<Linha>(listaOrdenada);                    
            MyListView.ItemsSource = Items;           
        }

        async void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            if (e.Item == null)
                return;

            //pega o ID do bairro que foi clicado
            Linha x = (Linha)e.Item;
            var idBairro = x.id;
            var sentidoBairro = x.sentido;

            //desmarca Item
            //((ListView)sender).SelectedItem = null;

            //chama a tela que mostra o calendario
            //await Navigation.PushAsync(new CalendarioOnibus(idBairro, sentidoBairro));
            var calendario = new CalendarioOnibus(idBairro, sentidoBairro);            
            await Navigation.PushAsync(calendario);
        }        
    }
}

Class with GET and SET

using System;
using System.Collections.Generic;
using System.Text;

namespace APPDiretoPonto.Model
{
    public class Linha
    {
        public string codigo { get; set; }
        public string nome { get; set; }        
        public int id { get; set; }
        public string sentido { get; set; }        
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace APPDiretoPonto.Model
{
    public class listaLinhas
    {
        public List<Linha> linhas { get; set; }
    }
}

1 answer

0

What happens is that a secondary thread cannot change the UI, try to replace its Task.Run(async () => await LoadItems(sentido)); for something like Device.BeginInvokeOnMainThread(() =>{ LoadItems(sentido) });

  • Lucas Miranda, your code quoted was very helpful. However, I had to make an adaptation because of the method I ended up calling next, being as follows: -> Device.Begininvokeonmainthread(async() => await Loaditems(sense));

  • cool, Renan! to keep the forum organized answer then your own question giving details of your solution process, ok?

Browser other questions tagged

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