几个IOS设备上没有调用DocumentPicker的DidPickDocument事件

时间:2018-01-17 06:58:39

标签: ios xamarin.forms

我有一个Xamarin表单应用程序,它使用UIDocumentMenuViewController来选择文档。

在iOS版本11.2.2和11.1.2上,不会调用事件“DidPickDocument”。然而,Iphone版本11.2.1和10.3.3一切正常。

以下是代码:

public async Task<ImportedFile> ImportFile(string descp){
    var ubiq = await Task.Run(() => NSFileManager.DefaultManager.GetUrlForUbiquityContainer(null));
    if (ubiq == null)
    {
        //  throw new Exception("iCloud not available");
    }
    test = new ImportedFile("", null, descp);
    TaskCompletionSource<ImportedFile> tcs = new TaskCompletionSource<ImportedFile>();

    var allowedUTIs = new string[] {
                UTType.PNG,
                UTType.PDF,
                UTType.Image };

    var activeController = UIControllerHelper.FindActiveViewController();

    //UIDocumentMenuViewController pickerMenu = new UIDocumentMenuViewController(new string[] { "org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc", "com.adobe.pdf" }, UIDocumentPickerMode.Import);
    UIDocumentMenuViewController pickerMenu = new UIDocumentMenuViewController(allowedUTIs, UIDocumentPickerMode.Import);
    pickerMenu.WasCancelled += (sender, args) => tcs.SetResult(null);

    pickerMenu.DidPickDocumentPicker += (sender, args) =>
    {
        args.DocumentPicker.WasCancelled += (docSender, docArgs) => tcs.SetResult(null);
        args.DocumentPicker.DidPickDocument += (docSender, docArgs) =>
        {
            ImportedFile file = null;

            try{
                var securityEnabled = docArgs.Url.StartAccessingSecurityScopedResource();
                var data = NSData.FromUrl(docArgs.Url);
                Helpers.Settings.strImagePath = docArgs.Url.Path;
                file = new ImportedFile(docArgs.Url.LastPathComponent, data.ToArray(), test.Description);
                tcs.SetResult(file);
                //App.GetFile(file);
                GetFileOnDownload(this, file);
            }
            catch (Exception excp){
                tcs.SetException(excp);
            }
            finally{
                docArgs.Url.StopAccessingSecurityScopedResource();
                    //   tcs.SetResult(file);
            }

        };
        activeController.PresentViewController(args.DocumentPicker, true, null);
    };

    pickerMenu.ModalPresentationStyle = UIModalPresentationStyle.Popover;
    pickerMenu.View.TranslatesAutoresizingMaskIntoConstraints = false;
        activeController.PresentViewController(pickerMenu, true, null);

    UIPopoverPresentationController presentationPopover = pickerMenu.PopoverPresentationController;
    if (presentationPopover != null){
        Thread.Sleep(4000);
        presentationPopover.SourceView = activeController.View;
        presentationPopover.PermittedArrowDirections = UIPopoverArrowDirection.Down;
        presentationPopover.SourceRect = activeController.View.Frame;
    }
    return await tcs.Task;
}

请建议。 在此先感谢

2 个答案:

答案 0 :(得分:0)

我认为UIDocumentMenuViewController类在较新的ios sdk版本(11.0+)中已弃用。尝试使用UIDocumentPickerViewController。 请参阅official documentation

答案 1 :(得分:0)

在iOS 11中也无法使用UIDocumentMenuViewController。在iOS 11.0以上版本中,使用UIDocumentPickerDelegate代替DidPickDocument EventHandler可以正常工作

class PickerDelegate : UIDocumentPickerDelegate
{
    public override void DidPickDocument(UIDocumentPickerViewController controller, NSUrl url)
    {
    }
}

用法:

var pickerMenu = new UIDocumentMenuViewController(s, UIDocumentPickerMode.Import);
pickerMenu.DidPickDocumentPicker += (sender, args) => {
    args.DocumentPicker.Delegate = new PickerDelegate();
    // Display the document picker
    PresentViewController(args.DocumentPicker, true, null);
};