Monotouch打开文档 - UIDocumentInterationController

jas*_*ark 5 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);
Run Code Online (Sandbox Code Playgroud)

  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;
                    }
                }
Run Code Online (Sandbox Code Playgroud)

Luk*_*uke 6

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

InvokeOnMainThread(delegate{
    PreviewController.PresentOpenInMenu(btnOpen,true);
});
Run Code Online (Sandbox Code Playgroud)

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,这将使用当前的viewcontroller来显示预览.我能想到的唯一其他改变是UIBarButtonItem尝试:而不是通过尝试来呈现:

PreviewController.PresentOpenInMenu(new RectangleF(320,320,0,500), this.View, true);
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助!

  • 另外我还意识到模拟器在测试过程中不会加载任何应用程序,因为它们在模拟器中不存在 - 有点过分. (2认同)