简体   繁体   English

将ObjectiveC类绑定到C#问题

[英]Binding ObjectiveC Class to C# problem

Binding ObjectiveC Class to C# problem 将ObjectiveC类绑定到C#问题

The monotouch project describes how to bind Objective-C Types for use with MonoTouch. monotouch项目描述了如何绑定Objective-C类型以与MonoTouch一起使用。 We failed to do this for the AdMob library (see also the monotouch-binding-for-admob blog at sabonrai dot wordpress dot com . 我们没有为AdMob库执行此操作(另请参阅sabonrai dot wordpress dot com中monotouch-binding-for-admob博客

So we decided to create the smallest possible test project. 所以我们决定创建尽可能小的测试项目。 We wrote a simple objc class with two simple methods, one that returns a string, and one that returns an integer. 我们用两个简单的方法编写了一个简单的objc类,一个返回一个字符串,另一个返回一个整数。

Here's the TstLib.h: 这是TstLib.h:

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

and the TstLib.m file: 和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

We've got a little objc console project to validate this library. 我们有一个小的objc控制台项目来验证这个库。 Here's the code: 这是代码:

#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);
}

So, let's define a binding file for the btouch utility. 所以,让我们为btouch实用程序定义一个绑定文件。

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

We then create a libTstLib.a and a TstLib.dll file with the btouch utility: 然后,我们使用btouch实用程序创建一个libTstLib.a和一个TstLib.dll文件:

/Developer/MonoTouch/usr/bin/btouch -o TstLib.dll TstCls.cs

We now create a new Monotouch window based iphone app 'ApiTest', add a Lib directory with the libTstLib.a and the TstLib.dll files, add a reference to this TstLib.dll and integrate our TstLib into the Main.cs: 我们现在基于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)
    {
    }
  }
}

This little project runs without the two Console.Writeline statements. 这个小项目在没有两个Console.Writeline语句的情况下运行。 It crashes without any output as soon as one of the Console.WriteLine statements is executed. 只要执行了一个Console.WriteLine语句,它就会在没有任何输出的情况下崩溃。

We've tried to be as concise as possible, still providing enough info to recreate the test case. 我们试图尽可能简洁,仍然提供足够的信息来重新创建测试用例。 We're very willing to provide any additional info to help fix this. 我们非常愿意提供任何其他信息来帮助解决这个问题。

Does anybody see why this does not work as expected? 有人知道为什么这不能按预期工作吗? We've limited ourselves to the bare minimum to test whether we can provide and use a binding for a minimal ObjC class. 我们将自己限制在最低限度,以测试我们是否可以为最小的ObjC类提供和使用绑定。

Unfortunately it fails. 不幸的是失败了。 And it fails in the same way as the MT_SampleAdMob project described in the monotouch-binding-for-admob blog. 它的失败方式与monotouch-binding-for-admob博客中描述的MT_SampleAdMob项目相同。

Our little project uses the btouch approach described at monotouch dot net under heading Binding_New_Objective-C_Types whereas the MT_SampleAdMob project uses the 'manual' approach described at the same location. 我们的小项目在标题Binding_New_Objective-C_Types下使用monotouch dot net中描述的btouch方法,而MT_SampleAdMob项目使用在同一位置描述的“手动”方法。

Both approaches fail in a similar matter. 两种方法在类似的问题上都失败了。 As soon as a class or instance method is called, the app just crashes without any output. 一旦调用了类或实例方法,应用程序就会在没有任何输出的情况下崩溃。

We've got no idea what can be done to pinpoint this problem and come to a solution. 我们不知道如何确定这个问题并找到解决方案。 Monotouch provides c# bindings for many of the ObjC classes, so it must be possible. Monotouch为许多ObjC类提供了c#绑定,因此它必须是可能的。 We've carefully studied the MonoTouch docs referenced above. 我们仔细研究了上面引用的MonoTouch文档。 We fail to see where either the MT_SampleAdMob or this btouch approach would deviate from the prescribed procedure, and yet both fail! 我们无法看到MT_SampleAdMob或这种btouch方法会偏离规定的程序,但两者都失败了!

So really, we desperately need some help here... 真的,我们在这里迫切需要一些帮助......

You likely didn't disable THUMB mode for your native library. 您可能没有为本机库禁用THUMB模式。 Ever since iOS SDK 3.0 the apple linker has problems linking Thumb libraries into larger projects. 从iOS SDK 3.0开始,Apple链接器在将Thumb库链接到更大的项目时遇到了问题。

You can disable thumb mode by opening your native library in Xcode and doing the following: 您可以通过在Xcode中打开本机库并执行以下操作来禁用拇指模式:

  1. Project -> Edit Project Settings 项目 - >编辑项目设置
  2. Type "thumb" in "Search in Build Settings" 在“在构建设置中搜索”中键入“拇指”
  3. Uncheck box 取消选中框
  4. Rebuild your native library. 重建您的本机库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM