【问题标题】:Xamarin Forms iOS UIImagePickerController not shown when called from a modal page (formsheet)从模式页面(表单)调用时未显示 Xamarin Forms iOS UIImagePickerController
【发布时间】:2021-07-12 19:59:28
【问题描述】:

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

【问题讨论】:

  • 您一次只能在 ViewController 上显示一个模式。您需要调用 PresentViewController,而不是在 RootViewController 上,而是在您现在显示的模式(您的表单)上。
  • 感谢您的帮助,我会试试这个并报告。

标签: ios xamarin uiimagepickercontroller


【解决方案1】:

感谢@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;
        }

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 2021-10-15
    • 1970-01-01
    • 2014-09-13
    • 2019-01-19
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多