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

0

Hello,

I have a problem.

Have my listView, where it brings the information as it should, but this information is only displayed when clicking on the screen. If you do not click on the screen, the information will not appear.

Does anyone have an idea of what might be happening?

Thanks in advance.

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 in 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; }
    }
}
    
asked by anonymous 28.10.2018 / 16:53

1 answer

0

What happens is that a secondary thread can not change the UI, try replacing its Task.Run(async () => await LoadItems(sentido)); with something like Device.BeginInvokeOnMainThread(() =>{ LoadItems(sentido) });

    
28.10.2018 / 20:38