In my binding library I've got an Objective-C selector:
/** * postGetBattery * Creates a SktScanObject and initializes it to perform a request for the * battery level in the scanner. */ -(void)postGetBattery:(DeviceInfo*)deviceInfo Target:(id)target Response:(SEL)response;
which I've bound as follows:
[Export ("postGetBattery:Target:Response:")] void PostGetBattery (DeviceInfo deviceInfo, NSObject target, Selector response);
My problem now is how I call this in my Xamarin.iOS application - providing the resonse selector. Creating a C# method with what looks like the right signature:
void onPostGetBatteryResponse (long result, ISktScanObject scanObject) { if (!SktScanErrors.SKTSUCCESS ((int)result)) { throw new Exception (string.Format ("Error in onPostGetBatteryResponse: result={0}", result)); } }
Produces the error:
/Users/james/P4/Novex Error CS1503: Argument `#3' cannot convert `method group' expression to type `MonoTouch.ObjCRuntime.Selector' (CS1503)
so presumably I have to either change/enhance my binding or create a selector associated with my callback method.
Can someone enlighten me please?
A selector is just like a method name. It's not an actual method. You have to do something like this:
private const string PostGetBatteryResponseSelectorName = "postGetBatteryResponseWithResult:scanObject:"; private static readonly Selector PostGetBatteryResponseSelector = new Selector(PostGetBatteryResponseSelectorName); [Export(PostGetBatteryResponseSelectorName)] private void onPostGetBatteryResponse (long result, ISktScanObject scanObject) { if (!SktScanErrors.SKTSUCCESS ((int)result)) { throw new Exception (string.Format ("Error in onPostGetBatteryResponse: result={0}", result)); } } private void SomeOtherMethod() { MethodThatWantsATargetAndSelector(this, PostGetBatteryResponseSelector); }
The error, I believe, is actually in your equivalent to that last bit of code. You can't pass onPostGetBatteryResponse to a function expecting a target and selector. You have to pass the target (your object) and a Selector object that matches the Export attribute.
Also make sure that the class containing this code inherits (directly or indirectly) from NSObject.
Answers
A selector is just like a method name. It's not an actual method. You have to do something like this:
The error, I believe, is actually in your equivalent to that last bit of code. You can't pass onPostGetBatteryResponse to a function expecting a target and selector. You have to pass the target (your object) and a Selector object that matches the Export attribute.
Also make sure that the class containing this code inherits (directly or indirectly) from NSObject.
Adam,
Thanks, that's just what I needed to know - to decorate the method with the correct Export.