微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我编写了一个代码,用于在 Xamarin IOS 中预览 PDF 文件现在如何使用此 QuickLook 框架在 Xamarin IOS 中注释此 PDF?

如何解决我编写了一个代码,用于在 Xamarin IOS 中预览 PDF 文件现在如何使用此 QuickLook 框架在 Xamarin IOS 中注释此 PDF?

UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
while (currentController.PresentedViewController != null)
    currentController = currentController.PresentedViewController;
UIView currentView = currentController.View;
QLPreviewController preview = new QLPreviewController();
QLPreviewItem item = new QLPreviewItemBundle(fileData.FileName,fileData.FilePath);
preview.DataSource = new PreviewControllerDS(item);

currentController.PresentViewController(preview,true,null);

解决方法

首先,创建一个继承自 QLPreviewControllerDataSource 的类。

public class QuickLookSource : QLPreviewControllerDataSource
{
    List documents;

    public QuickLookSource(List docs)
    {
        documents = docs;
    }

    public override nint PreviewItemCount(QLPreviewController controller)
    {
        return documents.Count;
    }

    public override IQLPreviewItem GetPreviewItem(QLPreviewController controller,nint index)
    {
        return new PreviewItem(documents[(int)index]);
    }
}

public class PreviewItem : QLPreviewItem
{
    NSUrl fileUrl;

    public override NSUrl ItemUrl => fileUrl;
    public override string ItemTitle => fileUrl.LastPathComponent;

    public PreviewItem(string url)
    {
        fileUrl = NSUrl.FromFilename(url);
    }
}

然后要显示预览,请创建一个新的 QLPreviewController 并将 DataSource 设置为您的数据源类。由于控制器可以预览多个文件,请确保在导航到控制器之前设置 CurrentPreviewItemIndex

var previewController = new QLPreviewController();
previewController.DataSource = new QuickLookSource(source.Documents);

previewController.CurrentPreviewItemIndex = indexPath.Row;
NavigationController.PushViewController(previewController,true);

// You can present modally instead
// PresentViewController(previewController,true,null);  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。