Search code examples
c#xamarinevent-handlingpicker

Event_Handler of the Done button of a picker


I have a xamarin picker with a list of items and I want to remove the picker when the "done" button is pressed on iPhone and "Oke" button on android.
I have the code to remove the picker. But i don't know what event_Handler that might by.

Code:

Picker picker = new Picker
    {
        Title = "What's in the slot?",
        VerticalOptions = LayoutOptions.CenterAndExpand
        //HorizontalOptions = LayoutOptions.Center 

    };

private void Displaypickerview(int row, int column)
    {
        if (status == "filling board")
        {
            foreach (string text in pickerText)
            {


picker.Items.Add(text);
        }
        foreach (string ore in oreLevels)
        {
            picker.Items.Add(ore);
        }


        picker.SelectedIndexChanged += (sender, args) =>
        {
            if (picker.SelectedIndex == -1)
            {

            }
            else
            {
                //change value of cell and button
                Picker picker = (Picker)sender;
                int index = picker.SelectedIndex;

                if (index < pickerText.Length)
                {
                    board[row, column].Text = pickerText[index - 1];
                }
                else {
                    board[row, column].Text = oreLevels[index - 1 - pickerText.Length];
                }
            }
        };
    }
    else if (status == "choosing item")
    {

    }

}

Example of what it looks like on iPhone:

Picker


Solution

  • Unfortunately, you cannot use the same event for Android and iOS in your case, because:

    Currently the Picker control sends the SelectedIndexChanged event on Android after the OK button is pressed. However, on iOS the event is sent whenever the user scrolls the picker and lets it stop on an item

    It's known issue, I take quote from here.

    You should combine SelectedIndexChanged and Unfocus events to achieve your goal. You can find some solutions here in this topic https://forums.xamarin.com/discussion/20847/picker-selection-event

    UPD: Looks like I didn't understand your question right. If I did it right now, so you have to use custom renderers and specify needed logic in them.

    For iOS. Create custom renderer inherited of PickerRenderer and implement something like that:

    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
    
            var extendedPicker = e.NewElement as ExtendedPicker;
            if (extendedPicker == null) return;
    
            var toolbar = new UIToolbar(new CGRect(0.0f, 0.0f, Control.Frame.Size.Width, 44.0f));
    
            toolbar.Items = new[]
            {
                new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace),
                new UIBarButtonItem("Done",
                    UIBarButtonItemStyle.Done,
                    delegate {
                        Control.ResignFirstResponder();
                    })
            };
    
            if (this.Control != null)
            {
                Control.InputAccessoryView = toolbar;
            }
        }
    

    For Andorid looks like it works from the box