I have created a binding for a java library that comes as part of an SDK for a scanner, model Honeywell Dolphin CT50. However, I have run into a bit of rut in my understanding.
The following example is given in Java. The Create method expects a interface implementation. However, i am not aware of a similar way to override the 'onCreated' method of the interface while still accessing the class-wide variables. This is were i am having the trouble, how can i do the following in C#.
Java:
private static BarcodeReader barcodeReader;
private AidcManager manager;
public void onCreate(Bundle savedInstanceState) {
// create the AidcManager providing a Context and a
// CreatedCallback implementation.
AidcManager.create(this, new CreatedCallback() {
@Override
public void onCreated(AidcManager aidcManager) {
manager = aidcManager;
barcodeReader = manager.createBarcodeReader();
}
});
}
In .NET, the compiler will not do as above and implement the interface as an anonymous type, thus I have to implement the interface. However, how can i then assign the class wide variables?
My C#:
NameSpace Scanner {
private static BarcodeReader barcodeReader;
private AidcManager manager;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
CreateCallback callback = new CreateCallback() { };
AidcManager.Create(this, callback.OnCreated(AidcManager aidcManager));
}
public class CreateCallback : AidcManager.ICreatedCallback
{
public IntPtr Handle
{
get
{
throw new NotImplementedException();
}
}
public void Dispose()
{
throw new NotImplementedException();
}
public void OnCreated(AidcManager p0)
{
// How can I initialize the manager and barcode reader variables in the class above from here?
}
}
}
You can create a callback implementation CreateCallback that has a other callback as constructor parameter successAction. This passed callback OnManagerCreated will be called in the OnCreated of CreateCallback.
Important Note: If you implement a Java interface, you have to inherit from Java.Lang.Object. Do not implement Handle and Dispose() on your own.
Activity
public class MyActivity : Activity
{
private static BarcodeReader _barcodeReader;
private AidcManager _manager;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
CreateCallback callback = new CreateCallback(OnManagerCreated) { };
AidcManager.Create(this, callback);
}
private void OnManagerCreated(AidcManager manager)
{
_manager = manager;
_barcodeReader = manager.CreateBarcodeReader();
}
}
Callback implementation
public class CreateCallback : Java.Lang.Object, AidcManager.ICreatedCallback
{
private Action<AidcManager> _successAction;
public CreateCallback(Action<AidcManager> successAction)
{
_successAction = successAction;
}
public void OnCreated(AidcManager p0)
{
_successAction(p0);
}
}
Other possibility
You can also implement the interface directly in your activity class like
public class MyActivity : Activity, AidcManager.ICreatedCallback
{
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With