微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Xamarin Forms iOS UIImagePickerController 从模态页面表单调用时未显示

我目前正在尝试使这个示例工作: 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;
        }

感谢您的帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。