Xamarin Forms iOS 从模态页面(表单)调用时未显示 UIImagePickerController

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

我目前正在尝试让这个示例工作: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker

但是在 iOS 上,UIImagePickerController 没有出现。我已经弄清楚了,这是因为调用页面是模态页面 (FormSheet)。我为模态页面设置了这个属性。

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

当我将页面改回正常页面(通过删除 Style)时,UIImagePickerController 工作正常。但是我需要从模态页面调用它。

我已经尝试将根 ViewController 设置为全屏。

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

但是结果是一样的。 无论如何都可以从模态页面显示 UIImagePickerController 吗?

感谢@Cheesebaron,我可以解决这个问题。 这对我有用。

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

感谢您的帮助!