List does not load database data with Xamarin.Forms and c#

Asked

Viewed 121 times

0

I created a list by code and I can’t load the data. I made this list by xaml and also not and I have another that works(xaml) and tried to do the same and nothing. When I go up the list appears:

Autorizador.Model.ItensLib

Below the code

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPageItens : ContentPage
    {
        List<ItensLib> _data { get; set; }
        int _idorcamento = 0;
        List<ItensLib> itens;
        DataService dataService = new DataService();

        public int IdOrcamento { get; set; }
        public MainPageItens()
        {
            InitializeComponent();
        }

        public MainPageItens(int idorcamento)
        {
            InitializeComponent();
            this.IdOrcamento = idorcamento;
            Task.Run(async () => await CarregaDados(IdOrcamento)).GetAwaiter().GetResult();

            ListView lv = new ListView() { HasUnevenRows = true, SeparatorVisibility = SeparatorVisibility.None };
            lv.ItemsSource = _data;

            this.Content = lv;
        }

        async Task CarregaDados(int idorcamento)
        {
            try
            {
                _data = await dataService.GetItensLibAsync(idorcamento);
            }
            catch(Exception ex)
            {
                string erro = ex.Message;
            }

        }

        private void listaItens_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            try
            {
                var libera = e.SelectedItem as Liberacao;
                _idorcamento = libera.IdOrcamento;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private async void btnVoltar_Clicked(object sender, EventArgs e)
        {
            await Navigation.PopModalAsync(true);
        }

        private async void btnOk_Clicked(object sender, EventArgs e)
        {
            //
        }
    }

With shampoo I do so:

<ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <StackLayout>
                <Editor x:Name="edtMotivo" HeightRequest="40" InputTransparent="False">

                </Editor>
            </StackLayout>
            <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                <Button x:Name="btnOK" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" HeightRequest="40" Text="OK" 
                    Clicked="btnOk_Clicked" FontSize="Small" BackgroundColor="Transparent" Image="" />
                <Button x:Name="btnVoltar" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" HeightRequest="40" Text="Cancelar" 
                    Clicked="btnVoltar_Clicked" FontSize="Small" BackgroundColor="Transparent" Image="editar.png" />
            </StackLayout>

            <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand" >
            <ListView HasUnevenRows="True" x:Name="listaItens"  ItemSelected="listaItens_ItemSelected" BackgroundColor="Aqua" SeparatorColor="Blue">

                <ListView.ItemTemplate>

                    <DataTemplate>

                        <ViewCell>
                            <!--<ViewCell.ContextActions>
                                    <MenuItem Clicked="OnAtualizar" CommandParameter="{Binding .}" Text="Atualizar" />
                                    <MenuItem Clicked="OnDeletar" CommandParameter="{Binding .}" Text="Deletar" IsDestructive="True" />
                                </ViewCell.ContextActions>-->

                            <StackLayout Padding="10,10" Orientation="Horizontal" HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand">
                                <Label Text="{Binding Produto}" HorizontalOptions="StartAndExpand" VerticalOptions="FillAndExpand" WidthRequest="270"/>
                                <Label Text="{Binding Qtde}" TextColor="Blue" HorizontalOptions="Center" WidthRequest="320"/>
                                <Label Text="{Binding UltCondicao}" HorizontalOptions="StartAndExpand" WidthRequest="520"/>
                                <Label Text="{Binding Unitario}" HorizontalOptions="StartAndExpand" WidthRequest="240"/>
                                <Label Text="{Binding Total}" HorizontalOptions="StartAndExpand" WidthRequest="240"/>
                            </StackLayout>

                        </ViewCell>

                    </DataTemplate>

                </ListView.ItemTemplate>

            </ListView>
            </StackLayout>

        </StackLayout>
    </ContentPage.Content>

EDIT1 I was able to bring the data, but the text was vertical. I did it by shampooing. See the screenshot of my device and then my shampoo?

inserir a descrição da imagem aqui

And my shampoo

<?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="Autorizador.MainPageItens">
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand" >
                <ListView HasUnevenRows="True" x:Name="listaItens"  ItemSelected="listaItens_ItemSelected" BackgroundColor="Aqua" SeparatorColor="Blue">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <!--<ViewCell.ContextActions>
                                        <MenuItem Clicked="OnAtualizar" CommandParameter="{Binding .}" Text="Atualizar" />
                                        <MenuItem Clicked="OnDeletar" CommandParameter="{Binding .}" Text="Deletar" IsDestructive="True" />
                                    </ViewCell.ContextActions>-->

                                <StackLayout Padding="10,10" Orientation="Horizontal" HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand">
                                    <Label Text="{Binding Produto}" HorizontalOptions="StartAndExpand" WidthRequest="270"/>
                                    <Label Text="{Binding Qtde}" TextColor="Blue" HorizontalOptions="Center" WidthRequest="320"/>
                                    <Label Text="{Binding UltCondicao}" HorizontalOptions="StartAndExpand" WidthRequest="520"/>
                                    <Label Text="{Binding Unitario}" HorizontalOptions="StartAndExpand" WidthRequest="240"/>
                                    <Label Text="{Binding Total}" HorizontalOptions="StartAndExpand" WidthRequest="240"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
           </StackLayout>
            <StackLayout>
                <Editor x:Name="edtMotivo" HeightRequest="40" InputTransparent="False">

                </Editor>
            </StackLayout>
            <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                <Button x:Name="btnOK" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" HeightRequest="40" Text="OK" 
                    Clicked="btnOk_Clicked" FontSize="Small" BackgroundColor="Transparent" Image="" />
                <Button x:Name="btnVoltar" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" HeightRequest="40" Text="Cancelar" 
                    Clicked="btnVoltar_Clicked" FontSize="Small" BackgroundColor="Transparent" Image="editar.png" />
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage> 

1 answer

0

It worked after I removed the padding from the stacklayout, that padding:

<StackLayout Padding="10,10" Orientation="Horizontal" HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand">

I switched from shampoo to code Behind and I did so and it worked:

protected override async void OnAppearing()
        {
            base.OnAppearing();

            //Task.Run(async () => await CarregaDados(IdOrcamento)).GetAwaiter().GetResult();
            await CarregaDados(IdOrcamento); 

            ListView lv = new ListView() { HasUnevenRows = true, SeparatorVisibility = SeparatorVisibility.Default, BackgroundColor=Color.White,SeparatorColor=Color.Gold };
            lv.ItemsSource = _data;

            lv.ItemTemplate = new DataTemplate(typeof(TextCell));
            lv.ItemTemplate.SetBinding(TextCell.TextProperty, "Produto");
            lv.ItemTemplate.SetBinding(TextCell.DetailProperty, "Unitario");

            this.Content = lv;

            //listaItens.ItemsSource = _data;
        }

Browser other questions tagged

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