Picker没有在Xamarin.Forms应用程序中显示

时间:2018-01-12 05:48:02

标签: c# xaml xamarin.forms

我对xamarin很新。 我正在开发我的第一个应用程序,其中, 一个。第一页显示userName,密码和登录按钮 湾第二页显示下拉列表,因此我使用了选择器控件。 但是选择器没有显示在我的应用程序屏幕上。(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>


    

非常感谢任何帮助..

更新1:这对我有用。但是我想知道为什么在使用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>

1 个答案:

答案 0 :(得分:0)

这对我有用

<?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>

documentation