Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Picker - Custom UIPickerView

Using Xamarin Forms, how can the default picker view be changed to show in a PopoverController? The code below throws "Popovers cannot be presented from a view which does not have a window." on iOS.

public class BindablePickerRenderer : PickerRenderer {
        private UIPickerView picker;

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

            this.picker = new UIPickerView() {
                Source = (UIPickerViewModel) new PickerSource(e.NewElement)
            };

            var popoverContent = new UIViewController();
            popoverContent.View = picker;


            var popoverController = new UIPopoverController(popoverContent);
            popoverController.PopoverContentSize = new SizeF(320, 180);
            popoverController.PresentFromRect(picker.Frame, this.Control.InputView, UIPopoverArrowDirection.Up, true);
        }
}
like image 877
Phil Avatar asked Nov 11 '22 05:11

Phil


1 Answers

Is this.Control.InputView the view from which you are presenting the popOverController? If so, it seems that at the time of OnElementChanged() execution, your this.Control.InputView is not a member of the window hierarchy, for whatever reason.

You may want to instead put your popOver-presenting action in an overridden MovedToWindow() or ViewDidAppear(). I don't know what type your this.Control.InputView is, but you'll need to override one of those methods in that type.

like image 147
NovaJoe Avatar answered Dec 19 '22 09:12

NovaJoe