2012-12-04 36 views
1

我是Monotouch的新手。最近,我正在研究一個Monotouch綁定項目,該項目綁定了一個將自己開發爲.NET框架庫的自定義iOS框架。我按照Xamarin的說明,但目前我遇到了無法解決的問題。這是我的代碼。Monotouch綁定 - 「不能從源類型轉換爲目標類型。」

**HEADER FILE IN OBJECTIVE C** 

*GRG.h* 
@interface GRG: NSObject {} 

// Shared instance 
+ (GRG*) sharedG; 

// Preference class 
@property (nonatomic, readonly) GRGPreferences *preferences; 

// Driver version 
@property (readonly,copy) NSString* driverVersion; 

// More parameters... 
@end 

*GRGPreferences.h* 
@interface GRGPreferences : NSObject <GRGPreferencesProtocol>{} 

// Enable DEBUG 
@property BOOL debugEnabled; 

// More parameters... 
@end 

*GRGPreferencesProtocol.h* 
@protocol GRGPreferencesProtocol <NSObject> 

// More parameters... 
@end 

將我的頭文件到這個

**API DEFINITION** 

[BaseType (typeof (NSObject))] 
interface GRG 
{ 
     [Static][Export("sharedG")] 
     GRG SharedG{ get; } 

     [Export("preferences")] 
     GRGPreferences Preferences{ get;} 

     [Export("driverVersion", ArgumentSemantic.Copy)] 
     string DriverVersion {get;} 
} 

[BaseType (typeof (GRGPreferencesProtocol))] 
public interface GRGPreferences 
{ 
     [Export("debugEnabled")] 
     bool DebugEnabled{ get; set;} 
} 

[BaseType(typeof (NSObject))] 
[Model] 
public interface GRGPreferencesProtocol 
{} 

在那之後,我創建了單一個測試應用程序測試新創建的庫並訪問我創造的價值。但是,我得到了一個錯誤。

Console.WriteLine(GRG.sharedG.DriverVersion); - 這工作正常。它返回適當的值。

GRGPreferences pref = GRG.SharedG.Preferences; - 錯誤:「無法從源類型轉換爲目標類型」。

Console.WriteLine(GRG.sharedG.Preferences.DebugEnabled); - 錯誤:「無法從源類型轉換爲目標類型」。

任何人都可以幫我嗎?

+0

你有沒有找到解決這個問題的方法?我有一個非常類似的問題(http://stackoverflow.com/questions/16906955/binding-a-return-type-that-c​​onforms-to-a-protocol) –

回答

1

從快看,我認爲這是你想要的東西:在實現您想要的協議

[BaseType (typeof (NSObject))] 
public interface GRGPreferences : GRGPreferencesProtocol { 

GRGPreferences類型從NSObject繼承。

+0

謝謝你的迴應。不幸的是它不起作用。我刪除了GRGPrefencesProtocol之間的鏈接,因此GRGPreferences只是一個NSObject,但它仍然不起作用。你有什麼其他的建議? –

+0

請更新您的原始問題與您的最新消息/定義,我會再看看。 – poupou

+0

你好poupou我只是更新我的代碼下面可以請你看看? –

相關問題