2011-11-23 47 views
0

我想通過我的應用程序打開一個* .epub文件,我不太明白如何使它與UIDocumentInteractionController類。我看過官方的IOS documentationexamples以及一些examples而不是the net,但我不明白這個類是如何工作的。這就是我要做的事,就是我實現什麼我不明白:如何使工作在MonoTouch中的UIDocumentInteractionController

我有一個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); 
      }); 


     } 
    } 
} 

最奇怪的是,如果我調試和檢查「DIC」(不委託)在調用PresentOpenInMenu()方法之前它顯示菜單(返回true),但在做完它之後,應用程序在Main.cs上爆炸,因爲我不明白autorelease的事情。

我有點迷路。有人可以幫助我理解這個課程,我該如何使它正確工作?提前致謝。

編輯:順便說一句,我也用了* .txt文件,結果也一樣。

+0

我剛試過你的例子,我得到的錯誤是: UIDocumentInteractionController:invalid scheme http。僅支持文件方案。 – slott

回答

0

它看起來像一個MonoTouch錯誤。設置UIDocumentInteractionController.Delegate(或WeakDelegate屬性,然後查詢它的值返回(這將失敗以後)。

我會考慮此問題,並更新這個答案,如果我可以提供一個解決方法(直到bug是正確地固定在未來的MonoTouch的釋放)

UPDATEUIDocumentInteractionController已經創建它自己的內部UIDocumentInteractionControllerDelegate,這樣你就不需要創建一個委託方法,如DidDismissOpenInMenu可作爲UIDocumentInteractionController本身事件

刪除你自己的委託(創建和設置)並使用事件,你應該沒問題。

UPDATE#2Delegate屬性返回null,因爲默認UIDocumentInteractionControllerDelegate不可用。它的意思是繼承和定製做你想做的事(並且不可用默認的一個未被正確註冊以便使用)。例如。

class MyDocumentInteractionControllerDelegate : UIDocumentInteractionControllerDelegate { } 

var dicDel = new MyDocumentInteractionControllerDelegate(); 

工作,在沒有出現NullReferenceException,但當然DidDismissOpenInMenu的不會做任何有趣。

+0

感謝您的回答poupou,我已經解決了這個問題。事實上,委託沒有實例,如果你想自己處理它的事件,你需要創建一個類繼承UIDocumentInteractionControllerDelegate類並覆蓋這些方法,但是如果你只想顯示options/preview/openIn菜單,你不需要完全需要代表。 –

+0

好吧,我正在尋找我的代碼,但我現在沒有在這裏,所以我明天會發布它,承諾。 關鍵是我錯過了在調用它之後如何保留對象,我需要將DocumentInteractionController的聲明作爲一個屬性,並將其放置爲[Export(「PresentOptionsMenu」,MonoTouch.ObjCRuntime.ArgumentSemantic.Retain )]屬性 –

+1

提供一個xxxDelegate是** optional **,允許您自定義主類(在這種情況下爲'UIDocumentInteractionController')。這在MonoTouch中始終得到支持 - 但基礎xxxDelegate類不會提供幫助,因爲它沒有做任何事情(直到您從中繼承)。一些類型,包括'UIDocumentInteractionController''也可以** **隱藏** xxxDelegate(它使用一個內部的),並讓你使用C#事件(這對.NET更友好)進行定製。 – poupou

相關問題