I am struggling to get my first Xamarin binding to work. I am trying to create a binding of the FlatUIKit library : https://github.com/Grouper/FlatUIKit.
I managed to create the fat static library (universal .a) of the above project. I used Objective Sharpie to generate the ApiDefinition.cs for me. I created a BindingProject in Xamarin using that ApiDefinition.cs (slightly modified to make the project compile). I took that new .dll and tried it in a new project. Calling methods from that .dll works fine and compiles. However, I get issues at runtime. All the static calls done to a FlatUI_UIColor return null. This makes this API unusable.
Here is the initial .h that contains the colors:
@interface UIColor (FlatUI)
- (UIColor *) colorFromHexCode:(NSString *)hexString;
- (UIColor *) turquoiseColor;
- (UIColor *) greenSeaColor;
- (UIColor *) emerlandColor;
- (UIColor *) nephritisColor;
- (UIColor *) peterRiverColor;
- (UIColor *) belizeHoleColor;
- (UIColor *) amethystColor;
- (UIColor *) wisteriaColor;
- (UIColor *) wetAsphaltColor;
- (UIColor *) midnightBlueColor;
- (UIColor *) sunflowerColor;
- (UIColor *) tangerineColor;
- (UIColor *) carrotColor;
- (UIColor *) pumpkinColor;
- (UIColor *) alizarinColor;
- (UIColor *) pomegranateColor;
- (UIColor *) cloudsColor;
- (UIColor *) silverColor;
- (UIColor *) concreteColor;
(UIColor *) asbestosColor;
(UIColor *) blendedColorWithForegroundColor:(UIColor *)foregroundColor backgroundColor:(UIColor *)backgroundColor percentBlend:(CGFloat) percentBlend;
@end
Below is the binding produced by Objective Sharpie:
[Category, BaseType (typeof (UIColor))]
public partial interface FlatUI_UIColor {
[Static, Export ("colorFromHexCode:")]
UIColor ColorFromHexCode (string hexString);
[Static, Export ("turquoiseColor"), Verify ("ObjC method massaged into getter property", "/Users/jhondel/Projects/iOS Libraries/FlatUIKit/Classes/ios/UIColor+FlatUI.h", Line = 14)]
UIColor TurquoiseColor { get; }
[Static, Export ("greenSeaColor"), Verify ("ObjC method massaged into getter property", "/Users/jhondel/Projects/iOS Libraries/FlatUIKit/Classes/ios/UIColor+FlatUI.h", Line = 15)]
UIColor GreenSeaColor { get; }
/// ...
[Static, Export ("blendedColorWithForegroundColor:backgroundColor:percentBlend:")]
UIColor BlendedColorWithForegroundColor (UIColor foregroundColor, UIColor backgroundColor, float percentBlend);
}
In ApiDefintion.cs, I get rid of the verify.
If it helps, this is the .m of the initial library:
@implementation UIColor (FlatUI)
// Thanks to http://stackoverflow.com/questions/3805177/how-to-convert-hex-rgb-color-codes-to-uicolor
+ (UIColor *) colorFromHexCode:(NSString *)hexString {
NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
if ([cleanString length] == 3) {
cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
[cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
[cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
[cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
}
if([cleanString length] == 6) {
cleanString = [cleanString stringByAppendingString:@"ff"];
}
unsigned int baseValue;
[[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
float red = ((baseValue >> 24) & 0xFF)/255.0f;
float green = ((baseValue >> 16) & 0xFF)/255.0f;
float blue = ((baseValue >> 8) & 0xFF)/255.0f;
float alpha = ((baseValue >> 0) & 0xFF)/255.0f;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
+ (UIColor *) turquoiseColor {
static UIColor *turquoise = nil;
static dispatch_once_t dispatchToken;
dispatch_once(&dispatchToken, ^{
turquoise = [UIColor colorFromHexCode:@"1ABC9C"];
});
return turquoise;
}
Any ideas why FlatUI_UIColor.GreenSeaColor (and the others) return null?
Thanks for your help.
UPDATE after @jstedfast comment:
Thanks for your reply.
I have built the fat library using the makefile in https://github.com/xamarin/monotouch-samples/blob/master/BindingSample/src/binding/Makefile.
It builds a Universal library (which gathers armv7 and i386).
When I import this .a in the binding project in Xamarin, it automatically generates the LinkWith. In my case:
[assembly: LinkWith ("libXMBindingLibrarySampleUniversal.a", LinkTarget.ArmV7 | LinkTarget.Simulator, ForceLoad = true)]
The library only uses "UIKit" and "Foundation" which, according to the docs, do not need to be specified in the LinkWith.
The generated .dll seems fine. I have access to its methods in my example project. I manage to create a FlatUIButton with a title and see the title when I run the Simulator. But the button cannot be clicked and is black since all the FlatUI_UIColor.X return null. I really do not know what is going on.