Building Cocoa UIs for OS X with C# and Mono

2019-01-30 20:39发布

问题:

Has anyone spent any time comparing the various Objective C bridges and associated Cocoa wrappers for Mono?

I want to port an existing C# application to run on OS X. Ideally I'd run the application on Mono, and build a native Cocoa UI for it.

I'm wondering which bridge would be the best choice.

In case it's useful to anyone, here are some links to bridges I've found so far:

  • CocoSharp - distributed with Mono on OS X - www.cocoa-sharp.com

  • Monobjc - better documentation than the others (in my opinion) - www.mono-project.com/CocoaSharp and www.monobjc.net

  • NObjective - (apparently) faster than the others - code.google.com/p/nobjective

  • MObjc / MCocoa - code.google.com/p/mobjc and code.google.com/p/mcocoa

  • ObjC# - www.mono-project.com/ObjCSharp

回答1:

The Mono team have released a new bridge called MonoMac.

It's essentially the desktop version of the MonoTouch framework they produced for the iPhone.

Miguel announced the new bridge on his blog here: http://tirania.org/blog/archive/2010/Apr-19.html

Once MonoMac matures, I suspect that it will become the bridge people use.



回答2:

I've been trying out Monobjc, NObjective and MObjc/MCocoa with F# lately and I will go with MObjc/MCocoa.

Monobjc is as you write best documented and packaged but compared to the other two very slow and does not "rethrow exceptions" from ObjC.

I believe that NObjective and MObjc/MCocoa are the only two that rethrow exceptions from ObjC and that's why I find them to be the only real alternatives.

As discussed here NObjective uses structs with inheritance which I find quite scary and made me finally choose MObjC/MCocoa.

(You might have a licence issue as well. Monobjc and NObjective is under LGPL, MObjc/MCocoa is under MIT ...)



回答3:

I think that NObjective is the best choise coz it have automatic code generation for Objective-C wrappers that can be easily configured to regenerate wrappers for further versions of Mac OS.



回答4:

OK, I'll ask: why would you want to do this? Cocoa's UI archtecture, including Cocoa-std delgates and Cocoa bindngs are closely tied to Objective-C. The combination of Interface Builder and Objective-C is remarkably productive. Although you can use Interface Builder with many bridges to Objective-C (including PyObjC, MacRuby, and many of the C# bridges), you will inevitably loose productivity due to any impedance mismatch between the framework and your language of choice. With C#, this mismatch is significant. Since you're writing a native UI, thus loosing any cross-platform advantage you would gain from C#, I would use Objective-C. For a competent C/C++/C#/Java/etc. programmer, it generally takes 2-3 days to become comfortable and productive in Objective-C.



回答5:

A fairly lengthy discussion of the various bridges has taken place on the Mono-OSX mailing list.

The discussion starts here with Miguel:

  • Announcing that Cocoa# will no longer be maintained by Novell.
  • Attempting to unify the remaining efforts in the community.

If you read the rest of the discussion, it appears that the Mono community is leaning towards either Monoobjc or MObjc / MCocoa; so if you're doing your own investigations these are probably the ones to focus on.



回答6:

Not going to ask why, since you have your reasons I am sure.

I would look at the Unity3D project and see if you can suss out how they did it. Looks, um, hard.