Sara Sultana
Sara Sultana

Reputation: 11

exception in Picker Xamarin

Im new to xamarin. I have tried to implement Picker, project builds fine but it keeps giving me the following runtime exception:

"System.ArrayTypeMismatchException: 'Attempted to access an element as a type incompatible with the array.'" im attaching my xaml, code behind and exception screenshot here. hope someone can help.

this is the xaml part :

          <Label Text="{Binding Source={x:Reference picker}, 
          Path=SelectedItem}"/> 

                        <Label x:Name="hasarnedeniLabel"></Label>
                        <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>

and the code behind :

    void OnPickerSelectedIndexChanged(object sender, EventArgs e)
    {

        var picker = (Picker)sender;
        int selectedIndex = picker.SelectedIndex;

        if (selectedIndex != -1)
        {
            hasarnedeniLabel.Text = (string)picker.ItemsSource[selectedIndex];


        }
    }

screenshot of exception

Upvotes: 0

Views: 308

Answers (1)

EvZ
EvZ

Reputation: 12169

Which version of Xamarin.Forms are you using?

Before Xamarin.Forms 2.3.4 there was no need to declare the array type in XAML:

<Picker x:Name="picker" Title="Select a monkey">
  <Picker.ItemsSource>
    <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.ItemsSource>

Generally speaking, you should use MVVM. So there will be a VM with your array bound to the UI control. Here is an official Xamarin guide that may help you to understand MVVM.

Here is an example:

<Picker
  Title="Select a monkey"
  ItemsSource="{Binding Monkeys}" />

In order to make it work you also need to create a ViewModel for the page containing this picker control and set the BindingContext of the page to the ViewModel:

public MainPage()
{
  InitializeComponent();
  this.BindingContext = new MainPageViewModel(); /* Which should contain public List<string> Monkeys { get; set; } */
}

Alternatively you could set the BindingContext in XAML:

<ContentPage
  xmlns:vm="clr-namespace:YourProject.ViewModelsNamespace"
..>
  <ContentPage.BindingContext>
    <vm:MainPageViewModel />
  </ContentPage.BindingContext>
</ContentPage>

Upvotes: 1

Related Questions