繁体   English   中英

选择器未在Xamarin.Forms应用中显示

[英]Picker is not getting displayed in Xamarin.Forms app

我对xamarin非常陌生。 我正在开发我的第一个应用程序,其中 第一页显示用户名,密码和登录按钮b。 第二页显示一个下拉列表,因此我使用了选择器控件。 但是选择器未显示在我的应用程序屏幕上。(SecondPage)

应用程式

 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>

这对我有用

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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