在Xamarin中绑定iOS委托时出错

时间:2013-03-20 17:22:55

标签: c# ios objective-c binding xamarin

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

使用文档和绑定生成器,我创建了以下绑定ApiDefintion:

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 ();

            [...]
}
}

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

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

    [...]
}

问题是我真的不确定我是否正确实现了initWithFrame:delegate:function。

每当我尝试使用以下代码时,我都会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;)

1 个答案:

答案 0 :(得分:2)

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

试试这个:

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

    ...
}

Here是关于如何在Xamarin.iOS中绑定Objective-C库的更多文档。特别参见3.3 Binding Constructors部分。