0

我坚持让我的 DocumentPicker 完全正常工作。现在它显示了视图控制器,但我不知道如何等待或获得结果。

在swift中,您只需编写void documentPicker(UIDocumentPickerViewController controller, didPickDocumentAtUrl...方法,当它完成时,它就会到那里。

但在 Xamarin 中,它一定没有那么简单。我已经编写了该方法,从我调用它的类以及在我的AppDelegate.cs类和我的Main.cs类中。似乎没有一个工作,除非我写错了。

我有的是这个....

public async Task<string> pickResume()
{
    string path = string.Empty;

    var controller = new UIViewController();
    var docVC = new UIDocumentPickerViewController(new string[] { "org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc" }, UIDocumentPickerMode.Import);
    UIViewController topController = getTopViewController();
    topController.PresentViewController(docVC, true, null); 

    return path;
}

 void documentPicker(UIDocumentPickerViewController controller, NSUrl didPickDocumentAtURL)
 {
     Console.WriteLine("done"); 
 }

getTopViewController()只是获取顶视图控制器的辅助方法,因此我可以展示 DocumentPicker

4

1 回答 1

3

想通了,这比我想象的要容易得多。

UIDocumentPickerViewController两个EventHandlersDidPickDocument所以WasCancelled我只是将它们分配给两个不同的方法并完成。

public async Task<string> pickResume()
{
    string path = string.Empty;
    var controller = new UIViewController();

    var docVC = new UIDocumentPickerViewController(new string[] { "org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc" }, UIDocumentPickerMode.Import);
    docVC.DidPickDocument += DocVC_DidPickDocument;
    docVC.WasCancelled += DocVC_WasCancelled;

    UIViewController topController = getTopViewController();
    topController.PresentViewController(docVC, true, null); 

    return await GetDocPath(new CancellationTokenSource());
}

private void DocVC_WasCancelled(object sender, EventArgs e)
{
    //Handle being cancelled 
}

private void DocVC_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
{
    //Handle document selection
}
于 2016-07-08T19:19:25.340 回答