How can we handle the done button click event for a Xamarin Forms Picker?

前端 未结 1 948
情话喂你
情话喂你 2020-12-10 19:47

I want to fire a click event on the Done button on a Picker in Xamarin Forms. I found some people having custom render for entry, but how can we implement the done button in

相关标签:
1条回答
  • 2020-12-10 20:16

    You can use a custom renderer to achieve this. Looking at the source code for the Picker on iOS, you can see that the 'Done' button is added to a UIToolbar. You can get a reference to the button and then handle its 'Clicked' event:

    public class MyPickerRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
    
            if(e.OldElement != null)
            {
                var toolbar = (UIToolbar)Control.InputAccessoryView;
                var doneBtn = toolbar.Items[1];
    
                doneBtn.Clicked -= DoneBtn_Clicked;
            }
    
            if(e.NewElement != null)
            {
                var toolbar = (UIToolbar)Control.InputAccessoryView;
                var doneBtn = toolbar.Items[1];
    
                doneBtn.Clicked += DoneBtn_Clicked;
            }
        }
    
        void DoneBtn_Clicked(object sender, EventArgs e)
        {
            Console.WriteLine("Clicked!!!!");
        }
    }
    
    0 讨论(0)
提交回复
热议问题