5
votes

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);
        }
}
1

1 Answers

1
votes

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.