简体   繁体   English

如何在MonoTouch中使UIDocumentInteractionController工作

[英]How to make work UIDocumentInteractionController in MonoTouch

I am trying to open a *.epub file throught my application and I don't quite understand how to make it with the UIDocumentInteractionController class. 我正在尝试通过应用程序打开* .epub文件,但我不太了解如何使用UIDocumentInteractionController类进行创建。 I have seen the official IOS documentation and examples and some of the examples over the net but I don't understand how that class works. 我已经在网上看到了IOS的官方文档示例以及一些示例 ,但我不了解该类的工作原理。 This is how I do it, what I achieve and what I dont understand: 这是我的工作方式,取得的成就和不了解的内容:

I have a UIView with a UIButton: 我有一个带有UIButton的UIView:

using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Drawing;

public class MyView : UIViewController
{
    UIButton myBtn;

    public MyView() :base()
    {
        View.Frame = new RectangleF(0,0,1024,768);

        var myRect = new RectangleF(300,300,100,50);

        myBtn = UIButton.FromType(UIButtonType.RoundedRect);
        myBtn.Frame = myRect;
        myBtn.TouchUpInside += delegate
        {
            var dic = UIDocumentInteractionController.FromUrl(new NSUrl("http://192.168.50.50:2020/myfile.epub"));
            var dicDel = new UIDocumentInteractionControllerDelegate();
            dic.Delegate = dicDel;

            InvokeOnMainThread(delegate
            {
                var result = dic.PresentOpenInMenu(myRect, View, true);
                //If I do this -> NullReferenceException because delegate is null (something about autorelease?? Don't know)
                if(!result) dic.Delegate.DidDismissOpenInMenu(dic);
            });


        }
    }
}

The weirdest thing is if I debug and inspect "dic" (without the delegate) before calling the PresentOpenInMenu() method it shows the menu (returning true) but just after doing it the app blows up on Main.cs because the autorelease thing I dont understand. 最怪异的事情是,如果我在调用PresentOpenInMenu()方法之前调试并检查“ dic”(没有委托),它将显示菜单(返回true),但是在执行完此操作后,应用程序在Main.cs上崩溃,因为我会自动释放东西不明白。

I am a little lost. 我有点迷茫。 Can someone help me understand this class and how can I make it work correctly? 有人可以帮助我理解该课程吗?如何使其正常工作? Thanks in advance. 提前致谢。

EDIT : By the way, I used a *.txt file too with same results. 编辑 :顺便说一句,我也使用* .txt文件,结果相同。

It looks like a MonoTouch bug. 看起来像是MonoTouch错误。 Setting the UIDocumentInteractionController.Delegate (or WeakDelegate property and then querying its value returns null (which will fail later on). 设置UIDocumentInteractionController.Delegate (或WeakDelegate属性,然后查询其值)将返回null (稍后将失败)。

I'll look into this bug and update this answer if I can provide a workaround (until the bug is properly fixed in a future release of MonoTouch). 如果可以提供解决方法,我将调查此错误并更新此答案(直到该错误在将来的MonoTouch版本中得到正确修复)。

UPDATE : UIDocumentInteractionController already creates it's own internal UIDocumentInteractionControllerDelegate so you do not need to create one. 更新UIDocumentInteractionController已经创建了它自己的内部UIDocumentInteractionControllerDelegate因此您无需创建一个。 The Delegate methods, like DidDismissOpenInMenu are available as events on UIDocumentInteractionController itself. DidDismissOpenInMenu这样的Delegate方法可作为UIDocumentInteractionController本身上的事件使用。

Remove your own Delegate (creation and set) and use the events and you should be fine. 删除自己的代表(创建和设置)并使用事件,就可以了。

UPDATE #2 : The Delegate property returns null since the default UIDocumentInteractionControllerDelegate is not usable as is. 更新#2Delegate属性返回null,因为默认的UIDocumentInteractionControllerDelegate无法按UIDocumentInteractionControllerDelegate使用。 It is meant to be inherited from and customized to do what you want to (and the unusable default one is not registred properly to be used). 它是要继承自定义的并且可以做您想做的事情( 无法使用的默认值未正确注册才能使用)。 Eg 例如

class MyDocumentInteractionControllerDelegate : UIDocumentInteractionControllerDelegate { }

and

var dicDel = new MyDocumentInteractionControllerDelegate ();

would work , as in no NullReferenceException , but of course DidDismissOpenInMenu won't be doing anything interesting. 可以正常工作 ,就像没有NullReferenceException ,但是DidDismissOpenInMenu当然不会做任何有趣的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM