尝试呈现其视图不在窗口层次结构问题中的QLPreviewController



我正试图使用UIDocumentInteractionController在Xamarin Forms for IOS平台中打开一个下载的文件,并通过Web服务访问特定于平台的类,但它给我的问题是"尝试呈现视图不在窗口层次结构中的QLPreviewController">

public void viewFile(string path)
{
var PreviewController = UIDocumentInteractionController.FromUrl
(NSUrl.FromFilename(path));
PreviewController.Delegate = new UIDocumentInteractionControllerDelegateClass
(UIApplication.SharedApplication.KeyWindow.RootViewController.PresentedViewController);
Device.BeginInvokeOnMainThread(() =>
{
PreviewController.PresentPreview(true);
});
}

以下是UIDocumentInteractionControllerDelegateClass代码:

public class UIDocumentInteractionControllerDelegateClass : UIDocumentInteractionControllerDelegate   {
UIViewController ownerVC;
public UIDocumentInteractionControllerDelegateClass(UIViewController vc)
{
ownerVC = vc;
}
public override UIViewController ViewControllerForPreview(UIDocumentInteractionController controller)
{
return ownerVC;
}
public override UIView ViewForPreview(UIDocumentInteractionController controller)
{
return ownerVC.View;
}
}

试试这个代码

var fileinfo = new FileInfo(path);
var previewController = new QLPreviewController();
previewController.DataSource = new PreviewControllerDataSource(fileinfo.FullName, fileinfo.Name);
UINavigationController controller = FindNavigationController();

if (controller != null)
{                     
controller.PresentViewController((UIViewController)previewController, true, (Action)null);
}

相关内容

最新更新