Reputation: 2029
I want to create a Xamarin forms (Android only) project (XAML UI, databinding and so forth). But my project runs on a special hardware which I want to access.
The manufacturer provides a SDK for Xamarin (dll). It works as expected when I use it in a Xamarin Android application.
Is it possible to add this DLL to the forms project? Simply adding the dll to the forms project works (project builds) but using it in code a different thing.
Two things I fail from start are:
The dll has a factory method to create a hardware access object. This expects an Android.Content.Context (normally the activity where the element is used) and an interface as parameter. The interface (I have to implement in my form) must implement IJavaObject
and a kind of callback method.
I don't have the source code (just the dll) so I can't simply create some interface which I could reference in the forms project.
Or do I have to write a kind of facade for the hardware dll providing interfaces which I could use in the forms project?
Upvotes: 1
Views: 797
Reputation: 17658
Is it possible to add this DLL to the forms project? Simply adding the dll to the forms project works (project builds) but using it in code a different thing.
Not really - as you mentioned: it's a hardware related component typically applicable to Android systems.
And; you don't really have to, which you address in your second question:
Or do I have to write a kind of facade for the hardware dll providing interfaces which I could use in the forms project?
Yes, I'll create an example.
If you want to access the component from the Forms
project, you can easily create a wrapper with an interface and call it from your Forms
project.
Nice thing about that, that if in the future it becomes available on other hardware - you just need to add the implementation.
In the android project:
[assembly: Xamarin.Forms.Dependency(typeof(NativeHelper))]
namespace Connector.Droid
{
public class NativeHelper : INativeHelper
{
public void CloseApp()
{
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
}
public MemStats GetMemStats()
{
return new MemStats()
{
Max = Java.Lang.Runtime.GetRuntime().MaxMemory(),
Free = Java.Lang.Runtime.GetRuntime().FreeMemory(),
Total = Java.Lang.Runtime.GetRuntime().TotalMemory()
};
}
}
}
In the Forms
project, define the interface:
public interface INativeHelper
{
void CloseApp();
MemStats GetMemStats();
}
and call it like this:
Xamarin.Forms.DependencyService.Get<INativeHelper>().CloseApp();
Upvotes: 1