Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Picker.Clear() System.ArgumentOutOfRangeException

$
0
0

Hey guys,
I just realized a strange behaviour in my app.
Situation: I got the following Data-structure: A customer got multiple projects and projects got multiple orders. In my app the user can select orders to create a report. A picker is used to select a customer. When a customer is selected a second picker is filled with all the customer projects. When a project is chosen I represent all orders in Label + Switch combinations.
This works fine.
But as you can imagine I use OnSelectedIndexChanged events to update the GUI. But here something strange happens on every device. (Not sure about Android, I am not able to test it at the moment.)
Customer_picker_OnSelectedIndexChanged I want to clear the project picker. But this always creates a System.ArgumentOutOfRangeException.

My Code looks like this:

private void Customer_picker_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        Projects_picker.Items.Clear(); // <- My App crashs here
        foreach (ApiProject proj in customer[Customer_picker.SelectedIndex].Projects)
            Projects_picker.Items.Add(proj.Name);
        Projects_picker.SelectedIndex = 0;
    }
     private void Project_selected()
    {
        //if (Customer_picker.SelectedIndex == -1)
        //    Customer();
        OrderLabel = new Label[customer[Customer_picker.SelectedIndex].Projects[Projects_picker.SelectedIndex].Orders.Count];
        Orderswitch = new Switch[customer[Customer_picker.SelectedIndex].Projects[Projects_picker.SelectedIndex].Orders.Count];
        int i = 0;
        Grid g = new Grid()
        {
            ColumnDefinitions = 
            {
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = GridLength.Auto },
            }
        };
        g.Children.Clear();
        foreach (ApiOrder o in customer[Customer_picker.SelectedIndex].Projects[Projects_picker.SelectedIndex].Orders)
        {
            OrderLabel[i] = new Label();
            OrderLabel[i].Text = o.Name;
            OrderLabel[i].VerticalOptions = LayoutOptions.Center;
            OrderLabel[i].TextColor = Color.FromHex("#FF4D82B8");
            Orderswitch[i] = new Switch();
            Orderswitch[i].VerticalOptions = LayoutOptions.Center;
            Orderswitch[i].HorizontalOptions = LayoutOptions.EndAndExpand;
            Orderswitch[i].HeightRequest = 65.0;
            Orderswitch[i].Toggled += switcher_Toggled;
            g.Children.Add(OrderLabel[i], 0, i);
            g.Children.Add(Orderswitch[i], 1, i);
            ++i;
        }
        OrderList.Children.Clear();
        OrderList.Children.Add(g);
    }

I get an "System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection." On the clear. But... how can a clear be out of range? I am confused.

Can anyone explain me this behaviour?
I really appreciate any help you can provide.
Julian


Viewing all articles
Browse latest Browse all 77050

Trending Articles