Xamarin Forms中的UIDocumentPickerViewController选择

时间:2016-07-08 19:02:29

标签: xamarin xamarin.ios xamarin.forms uidocumentpickervc

我坚持让我的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

1 个答案:

答案 0 :(得分:2)

想出来,它比我做出来容易得多。

UIDocumentPickerViewController有两个EventHandlersDidPickDocumentWasCancelled,所以我只是分配了两种不同的方法并完成了。

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
}