首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin.iOS UIDocumentPickerViewController

Xamarin.iOS UIDocumentPickerViewController
EN

Stack Overflow用户
提问于 2018-08-28 06:08:52
回答 1查看 987关注 0票数 3

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

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

代码语言:javascript
复制
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的文档选择器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-28 06:15:14

我最近这样做了,它在我的xamarin表单应用程序中运行得很好:

代码语言:javascript
复制
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,如下所示:

代码语言:javascript
复制
  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;
    }

然后添加文档选择的事件,如下所示:

代码语言:javascript
复制
 private void DocPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e)
    {
     //Action to perform on document pick
    }

在出现查询时恢复。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52051165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档