Common IT tools: good or bad?

Would you like to always work in the same environment? So that you do not have to jump back and forth on development environments and using tools. The ideal, of course, is still far away, but the IT world is striving for it. Today we talk about a single cross-platform API library. Join now!



When developing applications for iOS and Android using Xamarin, developers can access any native API using C #. These bindings not only help you use the appropriate APIs in C #, but also provide the powerful functions of this language, such as async / await, events, delegates, etc.

This is a huge advantage for developers because they will never have to work outside of C #, regardless of whether they write general business logic, user interface, or access native functions. One of the key features that programmers often want to see when developing cross-platform applications on Xamarin is the ability to access common native functions from common code without having to write their own layers of abstractions or find a plugin in free access created by the community.

Xamarin.Essentials embodies the dream of a single API for accessing features such as geolocation, sensor readings, secure storage, etc., for any application created on Xamarin!


APIs in Xamarin.Essentials


In the evaluation version, available today, the Xamarin.Essentials APIs provide access to more than 25 native functions from a single cross-platform API library, which can be accessed from the common code, regardless of how the user interface is created. This means that you can use the Xamarin.Essentials API using a single Xamarin.Android application or Xamarin.Forms application adapted for iOS, Android and UWP. Despite the large number of functions, this library is fully optimized for the fastest possible operation and minimal impact on the size of the application, since it is protected from the linker. This means that only the API and functions you use will be included in your application, and everything else will be deleted during compilation.

A trial version of the library, which uses the basic set of cross-platform APIs, can be installed into your application via NuGet:


The initial set of APIs was determined based on feedback from Xamarin developers on what features they would like to see in the cross-platform API library. Over time, we will expand this list.

Beginning of work


Getting started using the Xamarin.Essentials API is easy in any existing application by following a few simple steps:

1. Open an existing project or create a new one using the Blank App template in Visual Studio C # (for Android, iPhone and iPad, or cross-platform).

2. Add the Xamarin.Essentials NuGet package to the project:

  1. Visual Studio. In the Solution Explorer pane, right-click on the solution name and select Manage NuGet Packages. Find Xamarin.Essentials and install the package into ALL projects, including iOS, Android, UWP and .NET Standard Libraries.
  2. Visual Studio for Mac. In the Solution Explorer panel, right-click on the project name and select Add> Add NuGet Packages ... Find Xamarin.Essentials and install the package to ALL projects, including iOS, Android and .NET Standard Libraries.

3. Add a reference to Xamarin.Essentials in any of the C # classes to reference the API.

using Xamarin.Essentials; 

4. Xamarin.Essentials requires a little additional configuration for Android projects in order to gain access to special features:

Xamarin.Essentials must be initialized using the OnCreate method as part of MainLauncher or any other Activity in the Android project:

 Xamarin.Essentials.Platform.Init(this, bundle); 

To handle runtime permissions on Android, Xamarin.Essentials must get any OnRequestPermissionsResult. Add the following code to all Activity classes:

 public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } 

Done! Now you can start using the Xamarin.Essentials API in your application. Each API comes with complete documentation , including code snippets that you can copy into your application.

Be sure to read our Getting Started Guide for more information.

Compass assembly


During one of my speeches on the Microsoft Build 2018, I showed how to combine the Xamarin.Forms and Xamarin.Essentials API features in order to build a cross-platform compass in less than 10 minutes. It's as easy as adding multiple images and a small compass user interface in the app:

  <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Image HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Source="compass.png"/> <Image HorizontalOptions="Center" VerticalOptions="Center" Source="arrow.png" x:Name="ImageArrow"/> <Label Grid.Row="1" x:Name="LabelInfo" VerticalOptions="Center" HorizontalOptions="Center" /> </Grid> 

I was able to use the Xamarin.Essentials compass API in the code above to record events when the compass changes position, update the label text and rotate the image. All this is done with a few lines of code:

  using Xamarin.Forms; using Xamarin.Essentials; namespace MyCompass { public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); // Register for reading changes Compass.ReadingChanged += Compass_ReadingChanged; } void Compass_ReadingChanged(CompassChangedEventArgs e) { LabelInfo.Text = $"Heading: {e.Reading.HeadingMagneticNorth}"; ImageArrow.Rotation = e.Reading.HeadingMagneticNorth; } protected override void OnAppearing() { base.OnAppearing(); Compass.Start(SensorSpeed.Ui); } protected override void OnDisappearing() { base.OnDisappearing(); Compass.Stop(); } } } 


This is the simple way to create a compass application! You can find the complete source code and images on github.

Future plans


Xamarin.Essentials has been thoroughly tested, but we nevertheless ask you to leave feedback during the validity period of our evaluation version. Try out the library in your applications and report any problems you encounter when working with the Xamarin.Essentials GitHub repository .

We consider this library as the main element of the Xamarin platform and we know that this is an important building block for all developers and applications. Therefore, we will integrate the Xamarin.Essentials NuGet package into each iOS, Android and Xamarin.Forms template in Visual Studio and Visual Studio for Mac, so that developers can access these APIs from the very first stages of their projects.

See with my own eyes


You can see a full review of Xamarin, Xamarin.Forms and Xamarin.Essentials as part of the “Creating Mobile Applications with Visual Studio and Xamarin” talk on Microsoft Build 2018.



Additional Information


You can view our documentation containing a complete guide to getting started and using each Xamarin.Essentials feature. We also provided a complete list of API links that can be viewed online to learn about all the details of the Xamarin.Essentials APIs.

Source: https://habr.com/ru/post/415021/


All Articles