简体   繁体   中英

Xamarin Forms iOS UIImagePickerController not shown when called from a modal page (formsheet)

I'm currently trying to get this example work: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker

However on iOS, the UIImagePickerController is not showing up. I already have figured out, that this is because the calling page is a modal page ( FormSheet ). I set this properties for the modal page.

<Style x:Key="ModalPageStyle" TargetType="ContentPage" BasedOn="{StaticResource DefaultPageStyle}">
        <Setter Property="Shell.PresentationMode" Value="ModalAnimated" />
        <Setter Property="ios:Page.ModalPresentationStyle" Value="FormSheet" />
    </Style>

When I change the page back to a normal page (by removing the Style ), the UIImagePickerController works fine. However I need to call it from the modal page.

I already tried to set the root ViewController to fullscreen.

public Task<Stream> GetImageStreamAsync()
        {
            // Create and define UIImagePickerController
            imagePicker = new UIImagePickerController
            {
                SourceType = UIImagePickerControllerSourceType.PhotoLibrary,
                MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.PhotoLibrary),
                ModalPresentationStyle = UIModalPresentationStyle.FormSheet,
            };

            // Set event handlers
            imagePicker.FinishedPickingMedia += OnImagePickerFinishedPickingMedia;
            imagePicker.Canceled += OnImagePickerCancelled;

            // Present UIImagePickerController;
            UIWindow window = UIApplication.SharedApplication.KeyWindow;
            var viewController = window.RootViewController;
            if(viewController.ModalPresentationStyle != UIModalPresentationStyle.FullScreen)
                viewController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;

            viewController.PresentViewController(imagePicker, true, null);

            // Return Task object
            taskCompletionSource = new TaskCompletionSource<Stream>();
            return taskCompletionSource.Task;
        }

However the result is the same. Is there anyway at all to show the UIImagePickerController from a modal page?

Thanks to @Cheesebaron I could fix the problem. This is working on my end.

public Task<Stream> GetImageStreamAsync()
        {
            // Create and define UIImagePickerController
            imagePicker = new UIImagePickerController
            {
                SourceType = UIImagePickerControllerSourceType.PhotoLibrary,
                MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.PhotoLibrary),
                //ModalPresentationStyle = UIModalPresentationStyle.FormSheet,
            };

            // Set event handlers
            imagePicker.FinishedPickingMedia += OnImagePickerFinishedPickingMedia;
            imagePicker.Canceled += OnImagePickerCancelled;

            // Present UIImagePickerController;
            UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
            while (currentController.PresentedViewController != null)
                currentController = currentController.PresentedViewController;
   
            currentController.PresentViewController(imagePicker, true, null);

            // Return Task object
            taskCompletionSource = new TaskCompletionSource<Stream>();
            return taskCompletionSource.Task;
        }

Thanks for helping out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM