Call method on specific platform only - xamarin

I know I can call a platform-specific method by using:
DependencyService.Get<ISomething>();
But I have a method I need to call only on one platform (Android). Instead of creating an interface and implementing it on one platform only, is there a way of calling a method in one platform?
I'm asking because creating an interface makes sense when there will be more than one implementation. But when there will be only one - I would expect there to be a simpler way of doing it.

Have you tried using Xamarin.Forms built in RuntimePlatform? For example, if you needed to do something specifically for Android only:
if (Device.RuntimePlatform == Device.Android)
{
//Call your method
}

Related

Xamarin Forms - calling a shared code method from the platform project

I have read the two other questions on SO regarding this and I wanted to know if there is a good solution for that now / best practice.
Long story short, we use an SDK which is written natively and we've wrapped it so that it works on Xamarin.Android and Xamarin.iOS. It has asynchronous callback methods. I need to call a method in the shared code when a callback is received in the Android project for instance.
There's a lot of info for doing the opposite - using DependencyService. How about in my scenario? Does anyone have experience with an app like this and what's the best approach to keep code clean and do this using MVVM?
The options I know are:
Using a static App instance - this is what we currently do.
MessagingCenter
Anything else?
Actually I've never seen anyone recommend usage of MessagingCenter for anything else than communication between ViewModels so I am not sure it is recommended here. Also, I need to know the sender object type so I need a reference to the class in the platform specific project.
I would recommend you to use messagingCenter to pass data or call method between shared project and platform project. You can just send a new object instead of the class in the platform specific project.
Also, have a look at using eventhandler as I mentioned in this answer may help someone who want to call from the shared project into the platform specific one.
BTW, I mean you can even pass an object as TSender if it is not necessary to use:
MessagingCenter.Send<Object>(new object(), "Hi");
MessagingCenter.Subscribe<Object>(new object(), "Hi", (sender) =>
{
// Do something whenever the "Hi" message is received
});

Reverse Dependency Service

I have a Xamarin Forms Project. I have used Dependency Service to call the Platform specific method to fire a local notification on Android and a Alert on iOS.
My problem is now, how to handle an action on both notification(android) and alert(iOS). Is it possible to call a method of the shared project from the android or iOS project?
Do I need another approach? Does someone know what I have to do?
Just for clarification, I know how the ordinary dependency services works i.e. Call a method on android or iOS from the shared project!
I believe I had a similar scenario on my app. If you want to push something from your platform methods to your PCL you would need to make use of call backs and event delegates.
In Interface PCL:
event OnMessageHandlerCallback OnMessageEvent;
event OnErrorHandlerCallBack OnErrorEvent;
In Platform specific Class inheriting interface:
private OnMessageHandlerCallback callback = null;
private OnErrorHandlerCallBack errorCallBack = null;
public event OnMessageHandlerCallback OnMessageEvent;
public event OnErrorHandlerCallBack OnErrorEvent;
Have you tried just calling the method from your native project...? No special patterns necessary. The native iOS and Android projects have a direct reference to your shared project, so it can call the method directly.

android binding : Map java interface based callback to modern C#

I'm fine-tuning a library written in java for android that deals with bluetooth, that I will use in a Xamarin project with native binding.
Since it deal with I/O, most of the api contains async method and, cause we don't have access to the good stuff yet (lambdas), those async calls are dealt using callbacks (an interface to be implemented by the client). ie :
void connect(BluetoothPeripheral target, IConnectionCompletion handler);
Of course in C#, there are much better way to deal with async stuff : Action instead of creating a whole new class, or even better the Task with async/await.
My question is : What do I need to do to map those Callback based java methods to C# methods that will return Task or take and Action instead of the callback?

Do I need to recreate bindings, if the native framework changes?

I made an implementation change to one of the methods in the native framework. Would I need to recreate bindings, in this case ?
Short answer: likely not
Long answer: Depends, you would need to rebuild the binding only if this change you mention is in the public API signature of the method / property. This is because the bindings matches 1 : 1 (most of the time) what the native API surfaces so for example if your method used to return a NSString and now it returns another class or the selector name changes or the type of any of the parameters changes then yes.
You also would need to rebuild the binding if the binding dll bundles the native library you are using. If you are manually linking the native library (using the additional touch args in your app project) you should be fine.

Xamarin XLabs IOC and camera

I'm looking at https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs to try and get the camera available to my app.
I have this setup the same, but when I run my app the iOS app that calls my PCL is giving the error:
IResolver has not been set. Please set it by calling Resolver.SetResolver(resolver) method.
I don't know what this means or what exact code I need. I don't use IOC at all and I don't much care about it but I just want this camera to be available to my PCL. How can I get this camera available to my PCL when I'm not using MVC here.
If you wish to use the ViewModel then modify the constructor to take in IMediaPicker:
https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs#L62
public CameraViewModel(IMediaPicker mediaPicker)
{
this._mediaPicker = mediaPicker;
}
Remove the Setup function:
https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs#L156-L167
How you get the IMediaPicker implementation to PCL is up to you. IoC is the preferred (and easy) way but you can use other patterns as well. One would be to define a static Func<IMediaPicker> property on the ViewModel and set it in the platform specific projects.
IoC container is the easiest and quickest way to get it done and it would as simple as adding these lines at the application startup (e.g. AppDelegate's FinishedLaunching method):
var container = new SimpleContainer();
container.Register<IMediaPicker, MediaPicker>();
Resolver.SetResolver(container.GetResolver());

Resources