有没有办法混合使用 MonoTouch 和 Objective-C?

发布于 2024-08-06 06:10:40 字数 170 浏览 1 评论 0原文

我想知道是否有一种方法可以在一个项目中混合 C# 和 Obj-C 代码。具体来说,我想在 Obj-C 中使用 Cocos2D 作为 UI,并调用一些 MonoTouch C#-Library 来执行一些计算并返回一些值。有办法做到这一点吗?或者也许反过来,即在 MonoTouch 中构建并调用 Cocos2D 函数? 谢谢。

I'd like to know if there is a way to mix C# and Obj-C code in one project. Specifically, I'd like to use Cocos2D for my UI in Obj-C and call some MonoTouch C#-Library that does some computations and get some values back. Is there a way to do this? Or maybe the other way around, i. e. building in MonoTouch and calling Cocos2D-functions?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月蹉跎了容颜 2024-08-13 06:10:40

您描述的设置是可能的,但是管道并不像您在 MonoTouch 中完成整个项目时那么顺利。事实上,这就是我们引导 MonoTouch 的方式:我们采用了现有的 Objective-C 示例,然后用托管代码一一替换了这些位。

当这些样本发生位腐烂时,我们就丢弃了它们。

但您仍然可以完成此操作,使用 mtouch 的 --xcode 命令行选项为您生成示例程序,然后将所需的位从生成的 template.m 复制到 main.m 中。自定义您想要的组件,然后从那里启动 XCode 项目。

在您的开发周期中,您将继续使用 mtouch --xcode

The setup that you describe is possible, but the pipeline is not as smooth as it is when you do your entire project in MonoTouch. This is in fact how we bootstrapped MonoTouch: we took an existing Objective-C sample and we then replaced the bits one by one with managed code.

We dropped those samples as they bitrot.

But you can still get this done, use the mtouch's --xcode command line option to generate a sample program for you, and then copy the bits that you want from the generated template.m into your main.m. Customize the components that you want, and just start the XCode project from there.

During your development cycle, you will continue to use mtouch --xcode

罗罗贝儿 2024-08-13 06:10:40

回复:未知(谷歌):

我们实际上按照描述这样做了。

请参阅此页面以快速入门,但该页面上的最后一个代码段是错误的,因为它省略了“--xcode”参数。
http://monotouch.net/Documentation/XCode

嵌入 Mono-EXE/ 需要做什么将 DLL 转换为 Objective-C 程序的方法是使用 SharpDevelop 编译源代码,然后使用以下参数运行 mtouch:

/Developer/MonoTouch/usr/bin/mtouch --linksdkonly --xcode=output_dir MyMonoAssembly.exe

这仅适用于 MonoTouch 的完整版本。该试用版不允许使用“--xcode”参数。如果您希望 mtouch 在编译输出中保留未引用的类,则需要“--linksdkonly”参数,否则它会删除未使用的代码。

然后 mtouch 将您的程序集编译为本机 ARM 代码(文件扩展名 .s),并生成一个 XCode 模板,该模板加载 Mono-Runtime 和 XCode/ObjC 程序内的代码。您现在可以立即使用此模板并包含您的 Obj-C 代码或从“main.m”文件中提取运行时加载代码并将其插入到现有的 XCode 项目中。如果您使用现有项目,您还必须从 mtouch 制作的 xcode-output-dir 复制所有 .exe/.dll/.s 文件。

现在您已将 Mono-Runtime 和程序集加载到 XCode 项目中。要与程序集通信,必须使用 Mono-Embedding-API(不是 MonoTouch 的一部分,而是 Mono)。这些是 C 风格的 API 调用。有关详细介绍,请参阅此页面

另外,Mono-Embedding-API 文档可能会有所帮助。

您现在需要在 Obj-C 代码中执行的操作是进行 Embedding-API 调用。这些步骤可能涉及:获取应用程序域、获取程序集、获取程序集的图像、找到要使用的类、从该类实例化对象、查找类中的方法、调用对象的方法、将方法参数封装在C 数组并将它们传递给方法调用,获取并提取方法返回值。
上面的 embedding-api-doc-page 上有相关示例。

您只需要小心库的内存消耗,因为单声道运行时也会占用一些内存。

所以这就是从Obj-C到C#的方式。如果您想从 C#/Mono 调用 Obj-C 程序,则必须使用 MonoTouch 绑定,其描述为 这里

您还可以使用来自 embedding/P/Invoke-API 的纯 C 方法调用。

希望这能让你开始。

Re: unknown (google):

We actually did this as described.

See this page for a quick start, but the last code segment on that page is wrong, because it's omitting the "--xcode"-parameter.
http://monotouch.net/Documentation/XCode

What you have to do to embed your Mono-EXE/DLL into an Objective-C program is to compile your source with SharpDevelop, then run mtouch with these parameters:

/Developer/MonoTouch/usr/bin/mtouch --linksdkonly --xcode=output_dir MyMonoAssembly.exe

This only works with the full version of MonoTouch. The trial does not allow to use the "--xcode"-argument . The "--linksdkonly"-argument is needed if you want mtouch to keep unreferenced classes in the compiled output, otherwise it strips unused code.

Then mtouch compiles your assembly into native ARM-code (file extension .s) and also generates a XCode template which loads the Mono-Runtime and your code inside the XCode/ObjC-program. You can now use this template right away and include your Obj-C-code or extract the runtime loading code from the "main.m"-file and insert it into your existing XCode-project. If you use an existing project you also have to copy all .exe/.dll/.s files from the xcode-output-dir that mtouch made.

Now you have your Mono-Runtime and assembly loaded in an XCode-project. To communicate with your assembly, you have to use the Mono-Embedding-API (not part of MonoTouch, but Mono). These are C-style API calls. For a good introduction see this page.

Also the Mono-Embedding-API documentation might be helpful.

What you have to do now in your Obj-C-code is to make Embedding-API calls. These steps might involve: Get the application domain, get the assembly, get the image of the assembly, locate the class you want to use, instantiate an object from that class, find methods in class, call methods on object, encapsulate method arguments in C-arrays and pass them to the method-call, get and extract method return values.
There are examples for this on the embedding-api-doc-page above.

You just have to be careful with memory consumption of your library, as the mono runtime takes some memory as well.

So this is the way from Obj-C to C#. If you want to make calls from C#/Mono into your Obj-C-program, you have to use the MonoTouch-bindings, which are described here.

You could also use pure C-method calls from the embedding/P/Invoke-API.

Hope this gets you started.

半寸时光 2024-08-13 06:10:40

周末有人将 Cocos2D 移植到 .NET,因此您也可以在 .NET 上完成整个工作:

http://github.com/city41/CocosNet

Cocos2D 最初是一个 Python 项目,后来移植到 Objective-C,现在正在积极努力将其移植到 C#。它尚未完成,但作者正在接受补丁,这可能是一个更好的前进方向。

Over the weekend it emerged that someone has been porting Cocos2D to .NET, so you could also do the whole work on .NET:

http://github.com/city41/CocosNet

Cocos2D started as a Python project, that later got ported to Objective-C, and now there is an active effort to bring it to C#. It is not finished, but the author is accepting patches and might be a better way forward.

左耳近心 2024-08-13 06:10:40

从 MonoTouch 调用 Objective-C 看起来绝对是可能的。请参阅Objective-C 选择器示例

Calling Objective-C from MonoTouch definitely looks possible. See the Objective-C selector examples

南城旧梦 2024-08-13 06:10:40

你打电话给哪个图书馆?也许有 Objective-C 的等价物。

What library are you calling? Perhaps there's an Objective-C equivalent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文