在Xamarin中绑定iOS委托时出错



我正在尝试将TapForTap iOS SDK移植到我的Monotouch c#应用程序。

使用文档和绑定生成器,我创建了以下绑定apidedefinition:
namespace TapForTap {
[BaseType (typeof (NSObject))]
interface TapForTap {
    [...]
    [Static]
    [Export ("initializeWithAPIKey:")]
    void InitializeWithAPIKey (string key);
}
[BaseType (typeof (NSObject))]
[Model]
interface TapForTapAdViewDelegate {
    [Abstract]
    [Export ("rootViewController")]
    UIViewController RootViewController ();
    [...]
}
[BaseType (typeof (UIView))]
interface TapForTapAdView {
    [Static]
    [Export ("initWithFrame:delegate:")]
    NSObject Init (RectangleF frame, TapForTapAdViewDelegate d);
    [Export ("loadAds")]
    void LoadAds ();
    [Export ("stopLoadingAds")]
    void StopLoadingAds ();
            [...]
}
}

在我的项目中,我已经创建了一个类TapForTapDelegate实现TapForTapAdViewDelegate。

public partial class TapForTapAdDelegate : TapForTapAdViewDelegate {
    public override UIViewController RootViewController () {
        return Application.mainViewController;
    }
    [...]
}

问题是,我真的不确定我是否实现initWithFrame:delegate:函数正确。

每当我尝试使用以下代码时,我都会得到NSInvalidArgumentException Reason: +[TapForTapAdView initWithFrame:delegate:]: unrecognized selector sent to class 0x36dac4

bannerAd = (TapForTapAdView) TapForTapAdView.Init (new System.Drawing.RectangleF(0, y, 320, 50), new TapForTapAdDelegate());

我做错了什么?我已经试了几个小时了,还没有找到任何解决办法。我习惯使用Java,而不是c#和Objective-C;)

问题是init*方法被绑定为c#构造函数。

试试这个:

[BaseType (typeof (UIView))]
interface TapForTapAdView {
    [Export ("initWithFrame:delegate:")]
    IntPtr Constructor (RectangleF frame, TapForTapAdViewDelegate d);
    ...
}

这里有更多关于如何在Xamarin.iOS中绑定Objective-C库的文档。具体参见3.3绑定构造函数。

最新更新