简体   繁体   中英

Picker is not getting displayed in Xamarin.Forms app

I am very new to xamarin. I was developing my First app in which, a. First page displays userName, password and Login Button b. Second page displays a drop down , so I used picker control. But the picker is not displaying on my app screen.(SecondPage)

App.cs

 public class App : Application
{
     public App()
    {
        MainPage = new 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
    }
}

MainPage.xaml.cs

public class MainPage : ContentPage
{

    Entry UserName;
    Entry Password;
    Button LoginButton;

    public MainPage()
    {
        this.Padding = new Thickness(20, 20, 20, 20);
        StackLayout panel = new StackLayout
        {
            Spacing = 15
        };


        panel.Children.Add(UserName = new Entry
        {
            Placeholder = "Enter UserName",
        });

        panel.Children.Add(Password = new Entry
        {
            Placeholder = "Enter Password",
            IsPassword = true,
        });

        panel.Children.Add(LoginButton = new Button
        {
            Text = "Login",
            IsEnabled = true,
        });
        LoginButton.Clicked += OnLogin;
        this.Content = panel;


    }
    private async void OnLogin(object sender, EventArgs e)
    {
        await Navigation.PushModalAsync(new SecondPage());
    }
}

SecondPage.xaml.cs

 public partial class SecondPage : ContentPage
{

    public SecondPage()
    {

        StackLayout panel = new StackLayout
        {
            Spacing = 15
        };

        panel.Children.Add(new Label
        {
            Text = "Names of associates",
            FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label))
        });

        panel.Children.Add(new Label
        {
            Text = "Remarks",
            FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label))
        });

        this.Content = panel;
    }
}

SecondPage.xaml

<StackLayout Padding ="10" Orientation="Vertical" >
<Picker x:Name="picker" Title="Select a monkey">
 <Picker.ItemsSource>
   <x:Array Type="{x:Type x:String}">
     <x:String>Baboon</x:String>
     <x:String>Capuchin Monkey</x:String>
     <x:String>Blue Monkey</x:String>
     <x:String>Squirrel Monkey</x:String>
     <x:String>Golden Lion Tamarin</x:String>
     <x:String>Howler Monkey</x:String>
     <x:String>Japanese Macaque</x:String>
   </x:Array>
 </Picker.ItemsSource>


Any help is much appreciated..

Update 1: This works for me.But I wonder why I am getting exception when I use ItemSource.

SecondPage.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"
        x:Class="RegisterApp.SecondPage"
         Title="Picker Test">
<StackLayout Padding="10" Orientation="Vertical" >
    <Picker Title="Select a monkey">
        <Picker.Items>
            <x:String>Baboon</x:String>
            <x:String>Capuchin Monkey</x:String>
            <x:String>Blue Monkey</x:String>
            <x:String>Squirrel Monkey</x:String>
            <x:String>Golden Lion Tamarin</x:String>
            <x:String>Howler Monkey</x:String>
            <x:String>Japanese Macaque</x:String>
        </Picker.Items>
    </Picker>
 </StackLayout>
</ContentPage>

This works for me

<?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="ScratchPad.Pages.MainPage"
             Title="Picker Test">

    <StackLayout Padding="10" Orientation="Vertical" >
        <Picker x:Name="picker" Title="Select a monkey">
            <Picker.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>Baboon</x:String>
                    <x:String>Capuchin Monkey</x:String>
                    <x:String>Blue Monkey</x:String>
                    <x:String>Squirrel Monkey</x:String>
                    <x:String>Golden Lion Tamarin</x:String>
                    <x:String>Howler Monkey</x:String>
                    <x:String>Japanese Macaque</x:String>
                </x:Array>
            </Picker.ItemsSource>
        </Picker>
    </StackLayout>

</ContentPage>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM