开发者

Subclassing ObjC and classes with C# in MonoTouch and MonoDroid

开发者 https://www.devze.com 2023-03-28 04:29 出处:网络
I\'m about to evaluate MonoTouch and MonoDroid to unify our development but I\'m not sure if I got the subclassing of the two right. The documentation of both, MonoTouch and MonoDroid http://ios.xamar

I'm about to evaluate MonoTouch and MonoDroid to unify our development but I'm not sure if I got the subclassing of the two right. The documentation of both, MonoTouch and MonoDroid http://ios.xamarin.com/Documentation/API_Design#Design_Principles says you'd be able to create subclasses from ObjC and Java code.

Does that mean I can actually take existing none-framework code like a custom UI element or existing business logic and create a开发者_开发问答 subclass of it in C# to use in my project? An example would be great, because I don't the slightest clue of how that works with mono and C#.

Thanks!


Yes you can! Here's an example of Miguel's GlassButton of MonoTouch.

As you can see it inherits from the managed bindings of the native iOS UIButton and from there it can override its methods, just like you would expect in C#.

 public class GlassButton : UIButton {
    ...

    public override bool BeginTracking (UITouch uitouch, UIEvent uievent)
    {
       SetNeedsDisplay ();
       pressed = true;
       return base.BeginTracking (uitouch, uievent);
    }
    ...

You can do the same by binding your own UI (or not UI) code and then use it as normal C# code (and inherit, override... it).

Binding code for MonoTouch and Mono for Android is different, but once done, it's all the same (C# wise).


Does that mean I can actually take existing non-framework code like a custom UI element or existing business logic and create a subclass of it in C# to use in my project?

If I'm understanding you, Maybe. :-)

In MonoTouch, you can by binding the Objective-C type to a C# type, then subclassing the bound C# type. This is extremely flexible, and allows for subclassing ~any Objective-C type from C#. (There is no need for binding e.g. UIKit types, as those are already bound within monotouch.dll.)

The equivalent in Mono for Android -- subclassing a "random" Java class in C# -- isn't currently possible. C# code can only subclass types and implement interfaces found within Mono.Android.dll (which binds the types found in android.jar). Work is underway to relax this restriction in a future release.

0

精彩评论

暂无评论...
验证码 换一张
取 消