In which package can I find Xamarin IDevice - xamarin

I am writing a Xamarin Forms PCL application (both iOS and Android)
I have to pick an image from phone gallery.
I have read some documentation about this plugin:
Labs.Platform.Services.Media.MediaPicker
In my Common project, i have to put this code:
var device = Resolver.Resolve<IDevice>();
picker = DependencyService.Get<IMediaPicker>() ?? device.MediaPicker;
But I have an error on the first line: IDevice and Resolver Objects are not know. I think I am missing a reference or a using clause.
Thanks

You need to install the following Nuget package in all of your projects in the solution (i.e. PCL core project, iOS project, and the Android Project, and UWP project if using it):
https://www.nuget.org/packages/XLabs.Forms/
And you will need to add the following using statements:
using XLabs.Ioc;
using XLabs.Platform.Device;
using XLabs.Platform.Services.Media;

Related

NativeScript: use custom objective-c imported class into Xcode project in NativeScript

basically I have some Objective-c class already existing and I want to use them in my NativeScript projet. Currently I have added those files to the Xcode project target and I want to be able to call my objective-c code from nativeScript js. I've read the doc but I don't understand it. It seems so complicated. basically currently all I want to do is be able to present my custom view controller by calling probably something along
const vc = MyCustomViewController.alloc.initWithNibName("xib file")
page.frame.ios.controller.present(vc,true, nil)
Am I obligated to create a plugin for that? Am I obligated to use my objective-c class to build a framework in Xcode and then import the framework?
So I found out.
Actually what you need to do is first to compile your native iOS code into a framework. As per the documentation all your classes must inherit NSObject and all your function must be marked with #objc to be exposed to the objective-c Nativescript side if you write in swift. You will notice as well that in a framework your bundle is not the main bundle. In this example you can see how you can retrieve the bundle from the framework and load a xib from it.
Then you need to add your framework file to a Nativescript plugin. For that, you want to add the framework to the plugin's iOS folder yourPlugin/platform/iOS/yourFramework.framework.
then, you need to add your plugin to your app. You can add your local plugin by using the next command line. Notice the path end with the /src folder.
tns plugin add /path/to/yourplugin/src
Now, you can then call your native functions and classes without even importing them. Of course this works only on iOS. If you run your app on android, calling those methods will crash.
To show this viewController on your Nativescript side you will need to call the following code. By the way You can find documentation elsewhere to get a reference to the current page or frame object.
const controller = page.frame.ios.controller
const vc = IOMediaViewController.create()
vc.modalPresentationStyle = UIModalPresentationFullScreen
controller.presentViewControllerAnimatedCompletion(vc,true,null)

Using Xamarin Essentials in Xamarin UI Test project

I am trying to open Browser with a link inside my test method (application is launched prior to that). I installed Xamarin essentials Nuget, and per my current understanding I am not able to use this package outside xamarin project. I found some solutions (see below, using Mock object) but this does not seem to work.
var mockLaunch = new Mock();
IBrowser open = mockLaunch.Object;
open.OpenAsync("https://i389l.app.link/JFsYo6d1Y37", BrowserLaunchMode.SystemPreferred);
Does anyone have solution how to open browser (using real Android device) inside my test method? Thank you.

How to use System.Security.Cryptography in Xamarin.Forms PCL

By reference to this link https://developer.xamarin.com/api/namespace/System.Security.Cryptography/
May I know how do I include this in my Xamarim.Forms PCL project? When I include in, Visual Studio is giving error as the picture below
May I know if anybody has any idea how to solve this? Thanks.
The namespace is available both in Xamarin.iOS and Xamarin.Android. You could make platform specific implementations for both platforms and then resolve them with the DependencyService
You would have your interface for whatever you need in your PCL
public interface ICryptoService // or whatever
{
string Cipher(string stringToCipher);
string Decipher(string stringToDecipher);
}
and then implement these in your platform specific projects
using System.Security.Cryptography;
namespace MyApp.Droid
{
public class CryptoService : ICryptoService
{
// implement interface
}
}
To make the implementation visible to DependencyService you have to use the DependencyAttribute
[assembly: Xamarin.Forms.Dependency(typeof(MyApp.Droid.CryptoService)]
You can now obtain an instance in your PCL with
var cryptoService = DependencyService.Get<ICryptoService>();
and then use it. The steps for iOS are basically the same. For UWP you have to register the implementation manually, see here.
Edit:
Since it's likely that the implementation will be the same for all platforms, you could introduce a shared project and put the implementation there. All you have to do now is referencing the shared project from your iOS and Android projects.
Edit 2:
Adding a shared project to an existing Xamarin.Forms solution is quite easy. Just right-click your solution in VS, choose Add -> New Project... (I only have a german localized VS at hand at the moment, but it should be something in the lines of that). Now select Shared Project, give it a name and click OK, there will be a new shared project in your solution.
Now right-click your platform specific project and choose Add -> Reference.... The window to add a reference should open and on the left you can choose the source of the reference (Assemblys, Projects, Shared Projects, COM and Browse). Select Shared Project and then the project you just created. Any code file in your shared project will now be compiled with your platform specific project as if the code file was in the platform specific project (watch for namespaces!). Repeat for the other platform specific projects.

Issue with interacting with iPad accessory via Binding - Xamarin.iOS

I have a Barcode scanner accessory to which the iPad docks on to, hence i need to interface with the accessory to scan for barcode and use it in my application. To accomplish this, i followed the steps as enlisted here
Xamarin Hep - Link
So I went on to create a "Binding" project, added the native library i.e. ".a" file , created the APIDefinition and Structs using Sharpie and finally added the Binding project reference to my application.
In the next step, when I tried to deploy the app to iPad, encountered the error with regards to Native Linking.
In order to fix this, i mentioned the "Framework" in the LinkWith file like:
[assembly: LinkWith ("libBarCodeScannerSDK.a", SmartLink = true, ForceLoad = false, Frameworks="ExternalAccessory")]
Having done all the things stated above, I was able to deploy the app.But I am unable to make my application work with the Barcode scanner accessory. When I check the status of the scanner, its always returned as "NotConnected".
I am clueless as to how to go about debugging this issue and getting it resolved. Do appreciate, if someone can give me pointers on this.
Thanks,
Sid
most (if not all) of external accessories must set UISupportedExternalAccessoryProtocols key inside the info.plist file, see the info.plist file that the native Xcode example that came with your scanner SDK has and copy the UISupportedExternalAccessoryProtocols section to your Xamarin's app info.plist file.
Hope this helps!

Xamarin - Read Assets from Shared Project

I am using Xamarin for Android / iOS development. I have added a Shared Project within Xamarin solution. One of the functionality in the Shared project would be read assets within Android. However, AssetManager or Assets is not available from the Shared project. Any clue on how this can be used?
Maybe a bit late - hope it still helps somebody.
You can use
https://github.com/juergenkoller/PCLStorage
It has a GetFileFromAppBundleAsync function. You can place your file inside the asset folder of your Android project and do something like this:
var file = await FileSystem.Current.GetFileFromAppBundleAsync("your file.xyz", new CancellationToken(false));
var content = await file.ReadAllTextAsync ();

Resources