我试图通过我的应用程序打开一个* .epub文件,我不太明白如何使用UIDocumentInteractionController类。我已经看到官方的IOS documentation和examples以及examples上的一些the net,但我不明白该课程是如何运作的。我就是这样做的,我实现的目标和我不理解的东西:
我有一个带有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);
});
}
}
}
最奇怪的是,如果我在调用PresentOpenInMenu()方法之前调试并检查“dic”(没有委托),它会显示菜单(返回true)但是在完成之后,应用程序会在Main.cs上爆炸,因为autorelease我不明白的事情。
我有点失落。有人可以帮助我理解这个课程,我怎样才能使它正常工作?提前谢谢。
编辑:顺便说一句,我也使用了* .txt文件,结果相同。
答案 0 :(得分:0)
它看起来像MonoTouch错误。设置UIDocumentInteractionController.Delegate
(或WeakDelegate
属性然后查询其值将返回 null (稍后将失败)。
如果我可以提供解决方法(直到在未来的MonoTouch版本中正确修复了错误),我将调查此错误并更新此答案。
更新:UIDocumentInteractionController
已经创建了自己的内部UIDocumentInteractionControllerDelegate
,因此您无需创建一个。{1}}委托方法(如DidDismissOpenInMenu
)可作为UIDocumentInteractionController
本身的事件使用。
删除你自己的代表(创建和设置)并使用这些事件,你应该没事。
UPDATE#2 :Delegate
属性返回null,因为默认UIDocumentInteractionControllerDelegate
不可用。它意味着继承并自定义以执行您想要的操作(并且不可用的默认值未正确注册以供使用)。 E.g。
class MyDocumentInteractionControllerDelegate : UIDocumentInteractionControllerDelegate { }
和
var dicDel = new MyDocumentInteractionControllerDelegate ();
将工作,与no NullReferenceException
一样,但当然DidDismissOpenInMenu
将不会做任何有趣的事情。