Page Content does not appear in Xamarin Forms

Asked

Viewed 180 times

2

I’m having problems showing the contents of the pages. I created a Listview with some options, follow the code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace testeVale.Views
{
    public class Menu
    {
        public string itemMenu { get; set; }
    }
    public partial class MainPage : ContentPage
    {
        public List<Menu> Itens { get; set; }
        public MainPage()
        {
            InitializeComponent();

            this.Itens = new List<Menu>
            {

            new Menu { itemMenu = "Sobre Nós"},
            new Menu { itemMenu = "Cidades" },
            new Menu { itemMenu = "Eventos" },
            new Menu { itemMenu = "Contato" }

            };
            this.BindingContext = this;            
        }

         void ListViewMenu_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var menu = (Menu) e.Item;
            if (menu.itemMenu is "Sobre Nós")
            {
                Navigation.PushAsync(new sobreNos(menu));
            }
            if (menu.itemMenu is "Contato")
            {
                Navigation.PushAsync(new contato(menu));
            }
            /*DisplayAlert("Menu", string.Format("Você tocou no modelo'{0}'", menu.itemMenu),"Fechar");*/
        }
    }
}

On the "About Us" page for example, I’m trying to display the content but it’s only showing the 'back' button'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace testeVale.Views
{
    public partial class sobreNos : ContentPage
    {
        public sobreNos(Menu opcao)
        {
            InitializeComponent();
            this.Title = opcao.itemMenu;
            var label = new Label { Text = "This is a label." };
        }

        private void buttonVoltar_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new MainPage());
        }
    }
}

I also tried the 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="testeVale.Views.sobreNos">

    <ContentPage.Content>
        <StackLayout Padding="5,10">
            <Label TextColor="#77d065" FontSize = "20" Text="Sobre Nós" />
        </StackLayout>
    </ContentPage.Content>
    <Button x:Name="buttonVoltar" Text="Voltar" Clicked="buttonVoltar_Clicked" VerticalOptions="End"></Button>

</ContentPage>

Nothing appears, only the button, what I’m doing wrong?

1 answer

2

The file on Nos.xaml should be like this:

<?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="testeVale.Views.sobreNos">
    <StackLayout Padding="5,10">
        <Label TextColor="#77d065" FontSize = "20" Text="Sobre Nós" />
        <Button x:Name="buttonVoltar" Text="Voltar" VerticalOptions="End" />
    </StackLayout>
</ContentPage>

If you want to implement via code C#, would be like this:

    public class sobreNos : ContentPage {

        public sobreNos() {
            Content = new StackLayout {
                Padding = new Thickness(5, 10),
                Children = {
                    new Label { Text = "Sobre Nós", TextColor = Color.FromHex("#77d065"), FontSize = 20  },
                    new Button { Text = "Voltar", VerticalOptions = LayoutOptions.End }
                }
            };
        }
    }
  • I put inside the Stacklayout and appeared the content, what is the function of this command? Is similar to the Linearlayout of Android? Whenever I want to place content you will need to stay within this term?

  • The Stacklayout organizes your views (Buttons, Labels...) in a row of one dimension - either horizontal or vertical, one below the other or one beside the other. If you want the views to be in this layout, the most appropriate is to use the Stacklayout. I suggest a read on Layouts - Xamarin

Browser other questions tagged

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