将ObjectiveC类绑定到C#问题

gui*_*vho 4 c# iphone objective-c xamarin.ios admob

将ObjectiveC类绑定到C#问题

monotouch项目描述了如何绑定Objective-C类型以与MonoTouch一起使用.我们没有为AdMob库执行此操作(另请参阅sabonrai dot wordpress dot com上monotouch-binding-for-admob博客.

所以我们决定创建尽可能小的测试项目.我们用两个简单的方法编写了一个简单的objc类,一个返回一个字符串,另一个返回一个整数.

这是TstLib.h:

#import <Cocoa/Cocoa.h>
@interface TstCls : NSObject {
}
- (NSString *) Version;
- (int) GimmeAnInt;
@end
Run Code Online (Sandbox Code Playgroud)

和TstLib.m文件:

#import "TstCls.h"
@implementation TstCls
- (NSString *) Version {
    return @"I ain't got a version, I'm a poor lonesome cowboy...";
}
- (int) GimmeAnInt {
    return 110646;
}
@end
Run Code Online (Sandbox Code Playgroud)

我们有一个小的objc控制台项目来验证这个库.这是代码:

#import <Cocoa/Cocoa.h>
#import "../TstLib/TstCls.h"
int main(int argc, char *argv[])
{
    TstCls* tstCls = [[TstCls alloc] init];
    NSLog(@"version = %@", [tstCls Version]);
    NSLog(@"the int = %d", [tstCls GimmeAnInt]);
    return NSApplicationMain(argc,  (const char **) argv);
}
Run Code Online (Sandbox Code Playgroud)

所以,让我们为btouch实用程序定义一个绑定文件.

using MonoTouch.Foundation;
namespace TstLib {
  [BaseType (typeof (NSObject))]
    interface TstCls {
      [Export ("Version")]
      string Version ();
      [Export ("GimmeAnInt")]
      int GimmeAnInt ();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我们使用btouch实用程序创建一个libTstLib.a和一个TstLib.dll文件:

/Developer/MonoTouch/usr/bin/btouch -o TstLib.dll TstCls.cs
Run Code Online (Sandbox Code Playgroud)

我们现在基于iphone应用程序'ApiTest'创建一个新的Monotouch窗口,添加一个包含libTstLib.a和TstLib.dll文件的Lib目录,添加对此TstLib.dll的引用并将我们的TstLib集成到Main.cs中:

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using TstCls;
namespace ApiTest
{
  // -gcc_flags "-L${ProjectDir}/Lib -lTstLib -ObjC"
  // or
  // -gcc_flags "-L${ProjectDir}/Lib -lTstLib -force_load ${ProjectDir}/Lib/libTstLib.a"
  public class Application
  {
    static void Main (string[] args)
    {
      UIApplication.Main (args);
    }
  }
  // The name AppDelegate is referenced in the MainWindow.xib file.
  public partial class AppDelegate : UIApplicationDelegate
  {
    // This method is invoked when the application has loaded its UI and its ready to run
    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
      // If you have defined a view, add it here:
      // window.AddSubview (navigationController.View);

      TstLib.TstCls tstCls = new TstLib.TstCls ();
      Console.WriteLine ("TstLib.TstCls.Version() -> '{0}'", tstCls.Version ());
      Console.WriteLine ("TstLib.TstCls.GimmeAnInt() -> '{0}'", tstCls.GimmeAnInt ());
      window.MakeKeyAndVisible ();
      return true;
    }
    // This method is required in iPhoneOS 3.0
    public override void OnActivated (UIApplication application)
    {
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这个小项目在没有两个Console.Writeline语句的情况下运行.只要执行了一个Console.WriteLine语句,它就会在没有任何输出的情况下崩溃.

我们试图尽可能简洁,仍然提供足够的信息来重新创建测试用例.我们非常愿意提供任何其他信息来帮助解决这个问题.

有人知道为什么这不能按预期工作吗?我们将自己限制在最低限度,以测试我们是否可以为最小的ObjC类提供和使用绑定.

不幸的是失败了.它的失败方式与monotouch-binding-for-admob博客中描述的MT_SampleAdMob项目相同.

我们的小项目在标题Binding_New_Objective-C_Types下使用monotouch dot net中描述的btouch方法,而MT_SampleAdMob项目使用在同一位置描述的"手动"方法.

两种方法在类似的问题上都失败了.一旦调用了类或实例方法,应用程序就会在没有任何输出的情况下崩溃.

我们不知道如何确定这个问题并找到解决方案.Monotouch为许多ObjC类提供了c#绑定,因此它必须是可能的.我们仔细研究了上面引用的MonoTouch文档.我们无法看到MT_SampleAdMob或这种btouch方法会偏离规定的程序,但两者都失败了!

真的,我们在这里迫切需要一些帮助......

Geo*_*ton 6

您可能没有为本机库禁用THUMB模式.从iOS SDK 3.0开始,Apple链接器在将Thumb库链接到更大的项目时遇到了问题.

您可以通过在Xcode中打开本机库并执行以下操作来禁用拇指模式:

  1. 项目 - >编辑项目设置
  2. 在"在构建设置中搜索"中键入"拇指"
  3. 取消选中框
  4. 重建您的本机库.