如何使用Xamarin在iPad上实现UIDocumentInteractionController

gab6jxml  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(139)

我正在使用Xamarin开发一个iPad iOS 7.0应用程序,我想使用UIDocumentInteractionController来显示PDF。我使用了Stack Overflow上的各种示例来获得正确的实现,但没有任何效果。
这里有一个关于堆栈溢出的链接,我用它作为参考。
Monotouch open document - UIDocumentInterationController
我也使用了这个目标C教程,但一直无法正确地将此代码转换为C#. http://code.tutsplus.com/tutorials/previewing-and-opening-documents-with-uidocumentinteractioncontroller--mobile-15130
这是我的代码。我目前有一个工具栏按钮,当点击它时,应该触发PDF预览。当我点击按钮时,没有任何React。
我到底缺了什么才能让它起作用?

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    documentInteractionController = new UIDocumentInteractionController ();

    string fileName = "Content/test.pdf";
    string localURL = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
    NSUrl URL = new NSUrl(localURL, false);

    documentInteractionController = UIDocumentInteractionController.FromUrl (URL);
    documentInteractionController.Delegate = new UIDocumentInteractionControllerDelegateClass(this);

    toolbarButton.Clicked += delegate(object sender, EventArgs e){
        InvokeOnMainThread(delegate{
            documentInteractionController.PresentOpenInMenu(View.Frame, View, true);
        });
    };
}

public class UIDocumentInteractionControllerDelegateClass : UIDocumentInteractionControllerDelegate
{
    UIViewController viewC;

    public UIDocumentInteractionControllerDelegateClass(UIViewController controller)
    {
        viewC = controller;
    }

    public override UIViewController ViewControllerForPreview (UIDocumentInteractionController controller)
    {
        return viewC;
    }

    public override UIView ViewForPreview (UIDocumentInteractionController controller)
    {
        return viewC.View;
    }

    public override RectangleF RectangleForPreview (UIDocumentInteractionController controller)
    {
        return viewC.View.Frame;
    }
}
atmip9wb

atmip9wb1#

在研究了这个问题后,在另一个论坛上提出了同样的问题,并使用了不同的代码示例,我无法使用C#中的UIDocumentInteractionController来运行此代码。但是,我可以使用上面问题中链接的objective-c教程来运行此代码。这没有用,因为我需要C#中的代码,而不是objective-c。
在我的研究中,我发现了这篇关于Stack Overflow的文章,它使用了QLPreviewController。
Monotouch - Issue with QLPreviewController
这段代码正是我的应用程序所需要的。我对PDF的原始位置做了一个更改,它按预期工作。
这是代码,从上面的职位和修改,我正在使用打开一个PDF在一个快速预览控件,其中有"打开"的功能。
下面是控制器的代码:

QLPreviewController previewController= new QLPreviewController();             

previewController.DataSource=new MyQLPreviewControllerDataSource();     

this.PresentViewController(previewController,true, null);

下面是数据源的代码:

public class MyQLPreviewControllerDataSource : QLPreviewControllerDataSource { 

    public override int PreviewItemCount (QLPreviewController controller) {
        return 1;
    }

    public override QLPreviewItem GetPreviewItem (QLPreviewController controller, int index)
    {
        string fileName = @"example.pdf";
        var documents = NSBundle.MainBundle.BundlePath;
        var library = Path.Combine (documents,fileName);
        NSUrl url = NSUrl.FromFilename (library);
        return new QlItem ("Title", url);
    }
}

以下是QlItem的代码:

public class QlItem : QLPreviewItem 
    { 
        string title; 
        NSUrl uri; 

        public QlItem(string title, NSUrl uri) 
        { 
            this.title = title; 
            this.uri = uri; 
        } 

        public override string ItemTitle { 
            get { return title; } 
        } 

        public override NSUrl ItemUrl { 
            get { return uri; } 
        } 
    }
pb3s4cty

pb3s4cty2#

This maybe help someone if stumbled on this problem.
Opening files in other apps is not working in iOS simulator because it doesn't see any other apps. So you should be testing it on a physical device
Also you need to provide Uti for UIDocumentInteractionController like this:

documentInteractionController.Uti = @"com.adobe.pdf"

Used this xamarin forum topic for example.

xxb16uws

xxb16uws3#

我在毛伊岛的工作是:

MainThread.BeginInvokeOnMainThread(() =>
    {
        try
        {
            var documentInteractionController = new UIDocumentInteractionController();

            NSUrl URL = new NSUrl(fullFilename, false);

            documentInteractionController = UIDocumentInteractionController.FromUrl(URL);

            var view = Platform.GetCurrentUIViewController().View;
            documentInteractionController.PresentOpenInMenu(view.Frame, view, true);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

    });

fullFilename是文件名,包括您保存在应用程序沙箱位置中的文件的完整本地路径。

相关问题