c# - 如何使用 Xamarin 在 iPad 上实现 UIDocumentInteractionController

标签 c# ios ipad xamarin uidocumentinteraction

我正在使用 Xamarin 开发 iPad iOS 7.0 应用程序,我想使用 UIDocumentInteractionController 来显示 PDF。我在 Stack Overflow 上使用了各种示例来获得正确的实现,但没有任何效果。

这是我用作引用的 Stack Overflow 上的链接。

Monotouch open document - UIDocumentInterationController

我也使用了这个 objective-c 教程,但无法将此代码正确转换为 C#。 http://code.tutsplus.com/tutorials/previewing-and-opening-documents-with-uidocumentinteractioncontroller--mobile-15130

这是我的代码。我目前有一个工具栏按钮,单击该按钮应触发 PDF 预览。单击按钮时没有任何反应。

要使它起作用,我缺少什么?

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

最佳答案

在研究了这个问题后,在另一个论坛上提出了同样的问题,并使用了不同的代码示例,我无法使用 C# 中的 UIDocumentInteractionController 使这段代码正常工作。但是,我可以使用上述问题中链接的 objective-c 教程让它工作。这没有用,因为我需要 C# 而不是 Objective-C 中的代码。

在我的研究过程中,我在 Stack Overflow 上发现了这篇文章,它使用了 QLPreviewController。

Monotouch - Issue with QLPreviewController

此代码准确地提供了我的应用程序所需的内容。我对 PDF 的原始位置做了一处更改,它按预期工作。

这是从上面的帖子中提取和修改的代码,我正在使用它在具有“打开方式”功能的快速预览控件中打开 PDF。

Controller 代码如下:

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

关于c# - 如何使用 Xamarin 在 iPad 上实现 UIDocumentInteractionController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21913160/

相关文章:

c# - 如何在SSRS中动态进行水平合并

ios - UITableView 选择事件不起作用

ios - 复杂的 NSPredicates : comparing values on the most-recent object

objective-c - 在 iOS 应用程序中获取和显示来自 Facebook 的图像

c# - 如何通过代码测试某些条件然后验证或无效并显示错误消息?

c# - CollectionViewSource 违反 MVVM

c# - 动态创建类型的性能

ios - 当 UIWebView 反弹时重新加载页面

iphone - NSAutoReleasePool泄漏

iphone - "The argument is invalid."从iPhone配置实用程序安装IPA时