2

How can I get the selected item of my custom picker? It always goes back to the initial state "Sonstige" (= "Other"). So the value is not changeable.

I'm facing the problem for hours but have no further idea on how to fix it. I'm new to custom rendering.

I would like to use the DoneBtn_Clicked function to set the new value to the picker:

void DoneBtn_Clicked(object sender, EventArgs e){}

Screenshot of the custom picker:

screenshot


My code: Class PickerRendererIos.cs :

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

namespace DigitalNatives.iOS
{

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

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

            if (Control != null)
            {
                UIPickerView pickerView = (UIPickerView)Control.InputView;
                pickerView.WeakDelegate = this;
                pickerView.BackgroundColor = UIColor.White; //set the background color of pickerview

            }

        }


        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            Control.Layer.BorderWidth = 1;
            Control.BorderStyle = UITextBorderStyle.Line;
            Control.TextColor = UIColor.Blue;
        }

        [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,
            };
            var picker = this.Element;
            return label;
        }

    }
}
Mihail Duchev
  • 4,691
  • 10
  • 25
  • 32

1 Answers1

2

Cause:

Because you set the delegate of the pickerview pickerView.WeakDelegate = this;.But you did not implement all method in the delegate.

Solution:

You can override the click action of the button Done.Refer the following code

in the Custom Renderer

[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace xxx.iOS
{
   public class MyPickerRenderer:PickerRenderer,IUIPickerViewDelegate,IUIPickerViewDataSource
   {

    string SelectedValue;

    public MyPickerRenderer()
    {
    }

    public nint GetComponentCount(UIPickerView pickerView)
    {
        return 1;
    }

    public nint GetRowsInComponent(UIPickerView pickerView, nint component)
    {
        return Element.Items.Count;
    }

    [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,
        };
        var picker = this.Element;
        return label;
    }

    [Export("pickerView:didSelectRow:inComponent:")]
    public void Selected(UIPickerView pickerView, nint row, nint component)
    {
        Control.Text = Element.Items[(int)row];
        SelectedValue= Element.Items[(int)row]; 
    }

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

        if(Control!=null)
        {
            SelectedValue = Element.Items[0];

            UIPickerView pickerView = (UIPickerView)Control.InputView;
            pickerView.WeakDelegate = this;
            pickerView.DataSource = this;

            // get the button Done and rewrite the event
            UIToolbar toolbar = (UIToolbar)Control.InputAccessoryView;

            UIBarButtonItem done = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, (object sender, EventArgs click) =>
                {
                    Control.Text = SelectedValue;
                    toolbar.RemoveFromSuperview();
                    pickerView.RemoveFromSuperview();
                    Control.ResignFirstResponder();
                });

            UIBarButtonItem empty = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);

            toolbar.Items = new UIBarButtonItem[] { empty,done };
        }

    }

  }
}
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • Nice solution, but when I Try it i have the follow issues: - The selected item index doesn't update/selecteditem text returns the item 0 text - The Picker.SelectedIndexChanged event doesn't activate when changing the value (Probably related with the first issue) – Ameer Fares Mar 04 '20 at 06:25