My application does not give error, but nothing appears on the screen (Xamarin)

Asked

Viewed 339 times

2

Developing and learning Xamarin. I took an example of Macoratti and decided to implement, because it comes to consumption of REST with Xamarin. I did everything as it is on the author’s site and when I rotate, nothing appears on my smartphone(Samsung J5). When I leave the first StackLayout which is a label, it appears. But the other StackLayout shows nothing. Inside the first stack, I placed a component Entry along with the label, it appeared, but the placeholder was cut in half horizontally, that is, only the lower half appeared. I put down my code: Mainpage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Teste2"
             x:Class="Teste2.MainPage">

    <StackLayout Orientation="Vertical">
        <StackLayout Padding="5,5,0,0">
            <Label Text="Adicionar um Produto" TextColor="Green" />
        </StackLayout>
        <StackLayout Padding="10,0,10,0">
            <Entry x:Name="txtNome" Placeholder="Nome do produto" HorizontalOptions="Start" 
                    VerticalOptions="StartAndExpand" HeightRequest="30" WidthRequest="300" FontSize="Small"/>
            <Entry x:Name="txtCategoria" Placeholder="Categoria do produto" HorizontalOptions="Start" 
                    VerticalOptions="StartAndExpand" HeightRequest="30" WidthRequest="300" FontSize="Small" />
            <Entry x:Name="txtPreco" Placeholder="Preço do produto" HorizontalOptions="Start" VerticalOptions="StartAndExpand" 
                    HeightRequest="30" WidthRequest="300" FontSize="Small" />
            <Button HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" HeightRequest="40" Text="Adicionar/Atualizar Produto" 
                    Clicked="btnAdicionar_Clicked" FontSize="Small"/>
        </StackLayout>

        <!--<StackLayout Orientation="Vertical" Padding="10,5,10,0">
            <ListView x:Name="listaProdutos" ItemSelected="listaProdutos_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">
                                <Label Text="{Binding Nome}" HorizontalOptions="StartAndExpand"/>
                                <Label Text="{Binding Categoria}" TextColor="Blue" HorizontalOptions="Center"/>
                                <Label Text="{Binding Preco}" HorizontalOptions="End"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>-->
    </StackLayout>

</ContentPage>

Mainpage.xaml.Cs

public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }

App.Cs

public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            //MainPage = new Teste2.MainPage();
            MainPage = new NavigationPage(new Teste2.MainPage());
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

EDIT1 I did that and I can see my entry, but what I write or the placeholder is cut in half, which means I only read the bottom. I can’t read the whole text. What can that be? But I create more than one StackLayout, then nothing appears in the App. It seems that you are not accepting StackLayout nested.

StackLayout Padding="10,0,10,0">
            <!--<Label Text="Adicionar um Produto" TextColor="Green" />-->
                <Entry x:Name="txtNome" Placeholder="Nome do produto" HorizontalOptions="Start" 
                    VerticalOptions="StartAndExpand" HeightRequest="30" WidthRequest="300" FontSize="Small"/>
            <Entry x:Name="txtCategoria" Placeholder="Categoria do produto" HorizontalOptions="Start" 
                    VerticalOptions="StartAndExpand" HeightRequest="30" WidthRequest="300" FontSize="Small" />
        </StackLayout>
  • I observed, now in my cell, that when I close and open my app, gives this message: O Teste2.Android parou. I think the App is running in part, meaning it does the Deployment and so on, so I think. So do not click on the screen the components and for some reason, it only loads the label, but even so takes a long time to display.

  • By creating the components by Mainpage.xaml.Cs (in the class constructor), I can visualize and it’s even faster to deploy. I just need to see now, how to create events from the buttons and turn this page. Then, when I feel more comfortable with Xamarin, I go back to the shaman.

  • When I removed the Clicked events from the button and the Itemselected event from Listview, it worked, that is, I could see the components in my Cel. If I set the events, then it doesn’t work. I went to . Cs and added the events and even then, it didn’t work. I only did it with the Assi button: private void btnAdicionar_OnClicked(object sender, EventArgs e){}

1 answer

2

The problem has been solved. What was blurring was that both the button and Listview had click events and those events were only declared. Removing the event statement in Mainpage.xaml works, but I can’t call an event that way, but this is for another post.

Browser other questions tagged

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