Monotouch打开文档 - UIDocumentInterationController

时间:2010-12-23 21:01:33

标签: iphone cocoa-touch xamarin.ios

我想在我用iotouch写的iphone应用程序上打开一个文档   - 即在默认的PDF查看器中启动PDF文件。

我想我应该使用UIDocumentInterationController?

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

我已将以下内容放在viewcontroller(带工具栏)

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

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

1 个答案:

答案 0 :(得分:6)

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

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

如果仅此一项不起作用,我注意到的另一件事是你在委托类中创建一个新的视图控制器。它似乎没有被添加到代码中的任何位置,所以也许这就是为什么它没有显示。我使用的代码如下:

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

然后,这将使用当前的视图控制器来显示预览。我能想到的唯一其他改变不是通过UIBarButtonItem尝试来呈现:

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

我希望这有帮助!