Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
This repository was archived by the owner on May 1, 2024. It is now read-only.

Picker.cs Line 215 Throws Argument Out Of Range Exception when Binding Context gets set to Null #2499

@t4lm1d

Description

@t4lm1d

Description

In Picker.cs, when we close a popup dialog that contains a picker control inside the "SelectedIndexChanged" event, the Items collection has 0 elements and a SelectedIndex is still set. (This can occur when some portion of the code sets the BindingContext to null)

Under these conditions, we get an argument out of range exception within the UpdateSelectedItem() method on line 215:

void UpdateSelectedItem()
{
	if (SelectedIndex == -1) {
		SelectedItem = null;
		return;
	}
	if (ItemsSource != null) {
		SelectedItem = ItemsSource [SelectedIndex];
		return;
	}
	SelectedItem = Items [SelectedIndex]; //Line 215
}

Steps to Reproduce

  1. Set up a Picker control in a popup dialog
  2. In the SelectedIndexChanged event, set the BindingContext to null when the last element in the picker is chosen
  3. In the SelectedIndexChanged event close the popup when the last element in the Picker is chosen
  4. Run the code
  5. Select the last element
  6. Index out of bounds exception on line 215 of Picker.cs

Expected Behavior

I would expect there to be a check on line 215 of the Picker.cs that does not allow choosing an item by index value when there aren't any elements in the list.

Actual Behavior

Throws exception, app bombs

Reproductions

Picker picker = new Picker();
			picker.ItemsSource = new List<string> { "cat", "mouse", "rabbit" };
			picker.SelectedIndexChanged += (_, __) =>
			{
				picker.ItemsSource = null;
			};

			return new ContentPage()
			{
				Content = picker
			};

https://github.com/xamarin/Xamarin.Forms/files/2036809/ExceptionExample.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    in-progressThis issue has an associated pull request that may resolve it!t/bug 🐛

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions