将ObjectiveC类绑定到C#问题
将ObjectiveC类绑定到C#问题 monotouch项目描述了如何绑定Objective-C类型以与monotouch一起使用。我们未能为AdMob库执行此操作(另请参见 因此,我们决定创建尽可能小的测试项目。我们编写了一个简单的objc类,其中包含两个简单方法,一个返回字符串,另一个返回整数 以下是TstLib.h:将ObjectiveC类绑定到C#问题,c#,iphone,objective-c,xamarin.ios,admob,C#,Iphone,Objective C,Xamarin.ios,Admob,将ObjectiveC类绑定到C#问题 monotouch项目描述了如何绑定Objective-C类型以与monotouch一起使用。我们未能为AdMob库执行此操作(另请参见 因此,我们决定创建尽可能小的测试项目。我们编写了一个简单的objc类,其中包含两个简单方法,一个返回字符串,另一个返回整数 以下是TstLib.h: #import <Cocoa/Cocoa.h> @interface TstCls : NSObject { } - (NSString *) Version;
#import <Cocoa/Cocoa.h>
@interface TstCls : NSObject {
}
- (NSString *) Version;
- (int) GimmeAnInt;
@end
我们有一个小的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);
}
然后使用btouch实用程序创建libTstLib.a和TstLib.dll文件:
/Developer/MonoTouch/usr/bin/btouch -o TstLib.dll TstCls.cs
我们现在创建一个新的基于Monotouch窗口的iphone应用程序“Apiest”,添加一个包含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)
{
}
}
}
这个小项目在没有两个Console.Writeline语句的情况下运行。只要执行一个Console.Writeline语句,它就会崩溃,没有任何输出
我们已经尽可能地简洁,仍然提供足够的信息来重新创建测试用例。我们非常愿意提供任何其他信息来帮助解决这个问题
有人知道为什么这不能像预期的那样工作吗?我们将自己限制在最低限度,以测试是否可以为最小的ObjC类提供和使用绑定
不幸的是,它失败了。它失败的方式与admob博客monotouch binding中描述的MT_SamempleMob项目相同
我们的小项目使用monotouch dot net标题“Binding_New_Objective-C_Types”下描述的btouch方法,而MT_SamculeMob项目使用相同位置描述的“手动”方法
这两种方法在一个相似的问题上都失败了。只要调用一个类或实例方法,应用程序就会崩溃,没有任何输出
我们不知道可以做些什么来找出这个问题并找到解决方案。Monotouch为许多ObjC类提供c#绑定,因此这一定是可能的。我们仔细研究了上面提到的Monotouch文档。我们没有看到MT#Samembec Mob或此btouch方法会偏离规定的过程,但两者都失败了
所以说真的,我们迫切需要一些帮助…您可能没有为您的本机库禁用THUMB模式。自从iOS SDK 3.0以来,apple链接器在将THUMB库链接到更大的项目中时遇到问题 通过在Xcode中打开本机库并执行以下操作,可以禁用thumb模式:
杰夫,当然,你是对的。我忘了拇指的事。我曾看到有帖子提到这一点,但从来没有看到过关于这一点的清晰简洁的解释。在将这一点应用到我的小测试库后,iPhone测试成功了。因此,我现在将尝试为AdMob库解决这一问题,并将我的发现发布在这里。谢谢你的帮助莫莉。
/Developer/MonoTouch/usr/bin/btouch -o TstLib.dll TstCls.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)
{
}
}
}