0
votes

Am new in xamarin forms,i have a picker and populated it with items but it can't display the items.here is my code. Picker in Xaml

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

How i populated it in cs

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

Here is my full code 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());
        }

    }
}
2

2 Answers

1
votes

If you are using newest Xamarin.Forms, like version 2.5 or later, you probably should use ItemsSource instead of Items for data binding. In XAML it will look like this:

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

In CS like this:

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

picker.ItemsSource = monkeyList;

Instead of simple string you can also use complex types, check more here: https://developer.xamarin.com/guides/xamarin-forms/user-interface/picker/populating-itemssource/

0
votes

From the docs:

However, a Picker doesn't show any data when it's first displayed. Instead, the value of its Title property is shown as a placeholder on the iOS and Android platforms:

When the Picker gains focus, its data is displayed and the user can select an item: