【问题标题】:Xamarin.iOS UIDocumentPickerViewControllerXamarin.iOS UIDocumentPickerViewController
【发布时间】:2018-08-28 06:08:52
【问题描述】:

我有一个关于 iOS 文档选择器的问题。

我已包含 iCloud 容器/为我们的配置启用了 iCloud。还为 Xamarin.iOS 本身添加了所需的 Cloudkit 要求。但是,我在启动弹出菜单以显示 iOS 的浏览文件菜单时遇到问题。

var documentPicker = new UIDocumentPickerViewController(allowedUtis, UIDocumentPickerMode.Import);

documentPicker.DidPickDocument += DocumentPicker_DidPickDocument;
documentPicker.WasCancelled += DocumentPicker_WasCancelled;
documentPicker.DidPickDocumentAtUrls += DocumentPicker_DidPickDocumentAtUrls;
documentPicker.WasCancelled += DocumentPicker_WasCancelled;

private void DocumentPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e)
{
     var control = (UIDocumentPickerViewController)sender;
     foreach (var url in e.Urls)
         DocumentPicker_DidPickDocument(control, new UIDocumentPickedEventArgs(url));

         control.Dispose();
}

似乎事件 DidPickDocument / DidPickDocumentAtUrls 事件不会启动,除了设置我的配置、权利、info.plist 之外,是否有任何需要的选项/权限让我使用 iOS 的文档选择器?

【问题讨论】:

    标签: xamarin.forms xamarin.ios


    【解决方案1】:

    我最近做到了,它在我的 xamarin 表单应用程序中运行良好:

    private void ShowDocsPicker()
        {
            try
            {
                var docPicker = new UIDocumentPickerViewController(new string[]
                { UTType.Data, UTType.Content }, UIDocumentPickerMode.Import);
                docPicker.WasCancelled += DocPicker_WasCancelled;
                docPicker.DidPickDocumentAtUrls += DocPicker_DidPickDocumentAtUrls;
                var _currentViewController = GetCurrentUIController();
                if (_currentViewController != null)
                    _currentViewController.PresentViewController(docPicker, true, null);
            }
            catch (Exception ex)
            {
              //Exception Logging
            }
        }
    

    像这样获取当前的 UIViewController :

      public UIViewController GetCurrentUIController()
        {
            UIViewController viewController;
            var window = UIApplication.SharedApplication.KeyWindow;
            if (window == null)
            {
                return null;
            }
    
            if (window.RootViewController.PresentedViewController == null)
            {
                window = UIApplication.SharedApplication.Windows
                         .First(i => i.RootViewController != null &&
                                     i.RootViewController.GetType().FullName
                                     .Contains(typeof(Xamarin.Forms.Platform.iOS.Platform).FullName));
            }
    
            viewController = window.RootViewController;
    
            while (viewController.PresentedViewController != null)
            {
                viewController = viewController.PresentedViewController;
            }
    
            return viewController;
        }
    

    然后像这样添加 doc selected 事件:

     private void DocPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e)
        {
         //Action to perform on document pick
        }
    

    在查询时还原。

    【讨论】:

    • 您好,我的主要问题是事件 documentPicker.DidPickDocumentAtUrls 无法启动,您是否事先为权利做了任何事情,尤其是在配置方面。谢谢。
    • 尝试更新我的代码,看看它是否有效!
    • 当然没问题,如果您遇到任何进一步的问题,请告诉我
    猜你喜欢
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2018-05-30
    • 2021-11-03
    • 2014-08-21
    • 2020-03-23
    • 2020-05-11
    相关资源
    最近更新 更多