【问题标题】:Error when binding iOS delegate in Xamarin在 Xamarin 中绑定 iOS 委托时出错
【发布时间】:2013-03-09 22:58:47
【问题描述】:

我正在尝试将 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: 函数。

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

【问题讨论】:

    标签: c# ios objective-c binding xamarin


    【解决方案1】:

    问题在于 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部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多