Binding Obj-C Categories in MonoTouch
I am trying to bind WEPopover over to MonoTouch using btouch. It uses a UIBarButtonItem
category implementation to extend it's functionality to include presentation of the popover.
@interface UIBarButtonItem(WEPopover)
- (CGRect)frameInView:(UIView *)v;
- (UIView *)superview;
@end
How would I go about binding this into MonoTouc开发者_运维问答h using a btouch interface definition?
My old answer missed the fact that categories were used. That was pointed out in the comments and the link to the Xamarin documentation did cover this. Quoting:
In Objective-C it is possible to extend classes with new methods, similar in spirit to C#'s extension methods. When one of these methods is present, you can use the
[Target]
attribute to flag the first parameter of a method as being the receiver of the Objective-C message.For example, in MonoTouch we bound the extension methods that are defined on
NSString
when UIKit is imported as methods in theUIView
, like this:
[BaseType (typeof (UIResponder))]
interface UIView {
[Bind ("drawAtPoint:withFont:")]
SizeF DrawString ([Target] string str, PointF point, UIFont font);
}
From: http://docs.xamarin.com/ios/advanced_topics/binding_objective-c_types#Binding_Class_Extensions
The above example is what MonoTouch uses to bind drawAtPoint:withFont:
which is part of the NSString UIKit Additions
Here's a quick (and untested) definition for the above:
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
namespace MonoTouch.Popover {
[BaseType (typeof (UIBarButtonItem))]
public interface WEPopover {
[Export ("frameInView")]
RectangleF FrameInView (UIView view);
[Export ("superView")]
UIView SuperView { get; }
}
}
Then you use the btouch tool to compile the definition into a .dll usable from MonoTouch, e.g.
imac:tmp sebastienpouliot$ /Developer/MonoTouch/usr/bin/btouch we.cs
imac:tmp sebastienpouliot$ ls -l we.dll
-rwxr-xr-x 1 sebastienpouliot staff 5632 28 Aug 10:23 we.dll
I suggest you to read the existing documentation on how to bind existing ObjectiveC libraries. The document is available from: http://ios.xamarin.com/Documentation/Binding_New_Objective-C_Types
精彩评论