iphone - Monotouch打开文档-UIDocumentInterationController

标签 iphone cocoa-touch xamarin.ios

我想在我的 iPhone 应用程序上打开一个用单点触控编写的文档 - 即在默认 PDF 查看器中启动 PDF 文件。

我认为我应该使用 UIDocumentInterationController?

任何人对此有任何想法..

我已将以下内容放在 View Controller 上(带有工具栏)

但它不起作用:-(它什么也没做!!

string s = string.Format("{0}",strFilePath);

NSUrl ns = NSUrl.FromFilename (s);   

UIDocumentInteractionController PreviewController =
   UIDocumentInteractionController.FromUrl(ns);
PreviewController.Delegate =  new UIDocumentInteractionControllerDelegateClass();
PreviewController.PresentOpenInMenu(btnOpen,true);
<小时/>
  public class UIDocumentInteractionControllerDelegateClass : UIDocumentInteractionControllerDelegate
                {
                     public UIViewController FileViewController = new UIViewController();
                    public UIDocumentInteractionControllerDelegateClass ()
                    {
                    }

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

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

最佳答案

我要尝试的第一件事是确保当您呈现选项菜单时,它是在主线程上发生的:

InvokeOnMainThread(delegate{
    PreviewController.PresentOpenInMenu(btnOpen,true);
});

如果单独这样做不起作用,我注意到的另一件事是您正在委托(delegate)类中创建一个新的 View Controller 。它似乎没有添加到代码中任何地方的堆栈中,所以也许这就是它没有显示的原因。我使用的代码如下:

PreviewController.Delegate = new UIDocumentInteractionControllerDelegateClass(this);

...
...

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

然后,这将使用当前的 View Controller 来呈现预览。我能想到的唯一其他更改是,而不是从 UIBarButtonItem 尝试呈现:

PreviewController.PresentOpenInMenu(new RectangleF(320,320,0,500), this.View, true);

希望这会有所帮助!

关于iphone - Monotouch打开文档-UIDocumentInterationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4522444/

相关文章:

c# - RowSelected - NullReferenceException

iphone - 在 iPad (iOS 7) 上运行 iPhone 应用程序时,scalesPageToFit 无法正常工作

iphone - 这个 (NSNull*)controller == [NSNull null] 应该做什么?为什么不直接 Controller == nil?

ios - 我可以在图像 Assets 中定位特定的 iPhone 型号吗?

iphone - 将 PDF 加载到屏幕外的图层中

c# - 通过 MonoTouch 和 C# 将 AddTarget 用于 UIButton

c# - 统一 DLL 的 Monotouch.Dialog 替代方案

iphone - 允许用户在 iCloud 不可用时编辑/查看现有文档

iphone - 为什么加载 View Controller 时没有调用我的 initWithNib 方法?

objective-c - 计算消息计数 objective-c