Why is the Custom Picker SelectedItem always wrong?

ChristopherLeonChristopherLeon DEMember

Hello community,

how can I read the selected Item of my custom picker?
I'm facing the problem for hours but have no further idea on how to fix it.
I need the selected Value after clicking on the DoneBtn.

Thank you in advance!!

Class PickerRendererIos.cs :

[assembly: ExportRenderer(typeof(MyPickerRenderer), typeof(PickerRendererIos))]

namespace DigitalNatives.iOS
{

public class PickerRendererIos : PickerRenderer, IUIPickerViewDelegate 
{
    IElementController ElementController => Element as IElementController;
    public String test = "";

    public PickerRendererIos()
    {
    }

    [Export("pickerView:viewForRow:forComponent:reusingView:")]

     public UIView GetView(UIPickerView pickerView, nint row, nint component, UIView view)
    {
        UILabel label = new UILabel
        {
            TextColor = UIColor.Blue,
            Text = Element.Items[(int)row].ToString(),
            TextAlignment = UITextAlignment.Center,
        };
        test = label.Text;

        Console.WriteLine(Element.Items[(int)row]);

        return label;
    } 


    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
          base.OnElementChanged(e);

          if (Control != null)
          {
              UIPickerView pickerView = (UIPickerView)Control.InputView;
              pickerView.WeakDelegate = this;
              pickerView.BackgroundColor = UIColor.White;


        /*  var picker = (Picker)this.Element;
            picker.SelectedIndexChanged += (sender, ee) =>
            {
                //test = picker.Items[picker.SelectedIndex];
                test = picker.SelectedItem.ToString();
            }; */

        }

        if (e.OldElement != null)
          {
              var toolbar = (UIToolbar)Control.InputAccessoryView;
              var doneBtn = toolbar.Items[1];

              doneBtn.Clicked -= DoneBtn_Clicked;
          }

          if (e.NewElement != null)
          {
               var text = e.NewElement.SelectedItem;
              // test = text.ToString();

              var toolbar = (UIToolbar)Control.InputAccessoryView;
              var doneBtn = toolbar.Items[1];

              doneBtn.Clicked += DoneBtn_Clicked;
          }
    }

    void DoneBtn_Clicked(object sender, EventArgs e)
    {
        Console.WriteLine("Clicked!!!!");
        Element.SelectedItem = test;
    }
}

}

Answers

  • DipakPatilDipakPatil ✭✭ Member ✭✭

    Picker's index number starts from zero(0).

Sign In or Register to comment.