【问题标题】:Attempt to present QLPreviewController whose view is not in the window hierarchy issue尝试呈现其视图不在窗口层次结构中的 QLPreviewController 问题
【发布时间】:2020-09-11 12:17:12
【问题描述】:

我正在尝试使用 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;
            }
        }

【问题讨论】:

  • 请贴出相关代码

标签: c# ios xamarin.forms


【解决方案1】:

试试这个代码

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);
}

【讨论】:

    猜你喜欢
    • 2014-06-01
    • 1970-01-01
    • 2016-03-26
    • 2018-06-06
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多