I have a 3rd party Obj-C library (that I have written MonoTouch bindings for) that returns data via the NSDictionary
parameter within the UIImagePickerControllerDelegate
's FinishedPickingMedia
function (ZBar iPhone SDK for those interested)
I have bound the class of the instance that I expect to be stored in this dictionary (ZBarSymbol
).
As I expected, the MonoTouch runtime couldn't possibly check through all these collection instances when marshalling, and my MonoTouch code only sees an instance of NSObject
in the dictionary (If this assumption is wrong and it should be a ZBarSymbol
instance, can someone please let me know).
I attempted to manually marshal the NSObject
over into a ZBarSymbol
using the following code:
public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info)
{
var result = info[ZBarSDK.BarcodeResultsKey];
var symbol = result as ZBarSymbol;
if ( symbol != null )
{
// This never works obviously.
}
else
{
symbol = new ZBarSymbol(result.Handle);
Console.WriteLine("Data = " + symbol.Data);
}
}
The constructor of the ZBarSymbol taking the pointer works fine. However attempting to access any members (e.g. the Data
property results in the following exception:
MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.
Name: NSInvalidArgumentException
Reason: -[ZBarSymbolSet data]: unrecognized selector sent to instance 0x8d2960
at ZBar.ZBarSymbol.get_Data () [0x00000] in <filename unknown>:0
at ZBarMonoTouchTest.ZBarMonoTouchTestViewController+BarcodeReaderCallback.FinishedPickingMedia (MonoTouch.UIKit.UIImagePickerController picker, MonoTouch.Foundation.NSDictionary info) [0x00197] in /Users/tyson/Projects/ZBarMonoTouchTest/ZBarMonoTouchTestViewController.cs:112
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
at ZBarMonoTouchTest.Application.Main (System.String[] args) [0x00000] in /Users/tyson/Projects/ZBarMonoTouchTest/Main.cs:17
So is it possible to marshal these classes over within the application code? If so, how? Or even better, if someone could show me how to setup the bindings so this happens automatically (but I'm not sure this is possible).