【问题标题】:Picker not showing Items选择器不显示项目
【发布时间】:2017-09-28 14:18:40
【问题描述】:

我是 xamarin 表单的新手,我有一个选择器并用项目填充它,但它无法显示项目。这是我的代码。 Xaml 中的选择器

<StackLayout Margin="1">
                    <Picker x:Name="curr_picker" Title="select currency">
                    </Picker>
                    </StackLayout>

我如何在 cs 中填充它

curr_picker.Items.Add("UGX");
curr_picker.Items.Add("USD");
curr_picker.Items.Add("JPY");

这是我的完整代码 xaml

ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FX2.ForexRates"
             Title="Forex Rates"
             >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height=".3*" />
            <RowDefinition Height=".7*" />
        </Grid.RowDefinitions>
        <StackLayout Orientation="Vertical">
            <!--Frame 1 -->
            <Frame Margin="5" HasShadow="True" BackgroundColor="White" >
                <Frame.OutlineColor>
                    <OnPlatform x:TypeArguments="Color" Android="Gray" iOS="#DCDCDC" />
                </Frame.OutlineColor>
                <StackLayout Orientation="Vertical">
                    <Image  Source="searchbox.png" HorizontalOptions="EndAndExpand"/>
                    <Image  Source="flag_uganda.png" HorizontalOptions="StartAndExpand"/>
                </StackLayout>
            </Frame>

            <Frame Margin="5" HasShadow="True" BackgroundColor="White" >
                <Frame.OutlineColor>
                    <OnPlatform x:TypeArguments="Color" Android="Gray" iOS="#DCDCDC" />
                </Frame.OutlineColor>
                <StackLayout Orientation="Vertical">
                    <Label Text="UGANDA" HorizontalOptions="Center" TextColor="#5dade2"/>
                    <Image  Source="searchbox.png" HorizontalOptions="EndAndExpand"/>
                    <Label Text="Compare With" HorizontalOptions="Center" TextColor="#5dade2"/>
                    <StackLayout Margin="1">
                    <Picker x:Name="curr_picker" Title="select currency">
                    </Picker>
                    </StackLayout>
                    <Picker x:Name="option_picker" Title="BUY or SELL">
                    </Picker>
                    <Button Text="VIEW RATES" BackgroundColor="#0711a7" HorizontalOptions="FillAndExpand" TextColor="White" HeightRequest="65" Clicked="Button_Clicked"/>
                </StackLayout>
            </Frame>

        </StackLayout>


    </Grid>
</ContentPage>

cs

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

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace FX2
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ForexRates : ContentPage
    {
        public ForexRates()
        {
            InitializeComponent();

            curr_picker.Items.Add("UGX");
            curr_picker.Items.Add("USD");
            curr_picker.Items.Add("JPY");

            option_picker.Items.Add("BUY");
            option_picker.Items.Add("SELL");


        }

        private void Button_Clicked(object sender, EventArgs e)
        {
         //await Navigation.PushAsync(new MainActivity());
        }

    }
}

【问题讨论】:

    标签: c# xamarin.forms picker


    【解决方案1】:

    如果您使用的是最新的 Xamarin.Forms,例如 2.5 或更高版本,您可能应该使用 ItemsSource 而不是 Items 进行数据绑定。在 XAML 中,它看起来像这样:

    <Picker x:Name="picker">
      <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:Array>
      </Picker.ItemsSource>
    </Picker>
    

    在 CS 中是这样的:

    var monkeyList = new List<string>();
    monkeyList.Add("Baboon");
    monkeyList.Add("Capuchin Monkey");
    monkeyList.Add("Blue Monkey");
    
    picker.ItemsSource = monkeyList;
    

    您还可以使用复杂类型来代替简单的字符串,请在此处查看更多信息: https://developer.xamarin.com/guides/xamarin-forms/user-interface/picker/populating-itemssource/

    【讨论】:

      【解决方案2】:

      来自docs

      但是,选择器在首次显示时不会显示任何数据。相反,其 Title 属性的值在 iOS 和 Android 平台上显示为占位符:

      当 Picker 获得焦点时,会显示其数据,并且用户可以选择一个项目:

      【讨论】:

      • thnx@jason,但我的从来没有获得焦点,项目根本不显示。
      猜你喜欢
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 2020-08-30
      相关资源
      最近更新 更多