At Build, the Xamarin team announced the preview release of Xamarin.Essentials.
This library brings the essential cross-platform APIs used by mobile applications into one package.
With Essentials, there is no longer a need to search for a separate plugin for every common device feature.
Before Xamarin.Essentials, developers had to search through a long list of existing plugins and sometimes choose between several packages offering the same feature.
The first version supports:
- Accelerometer: allows you to monitor the device's accelerometer sensors which indicate the acceleration of the device in a three-dimensional space.
- App Information: provides information on the application: package name, version number, build number.
- Battery: allows you to have information about the battery of the equipment (if it is charged, the remaining % percentage ...)
- Clipboard: allows access to the hardware clipboard
- Compass: allows access to the geographical position of the user
- Connectivity: connectivity information can be retrieved (is it 4G or Wifi ???)
- Data Transfer: allows access to data sharing APIs: share text, email...
- Device Information: allows you to retrieve all information about the hardware: model, name, version, platform...
- File System Helpers: allows you to access the file system: create a file/folder, delete a folder...
- Email: allows you to open the default messaging application
- Flashlight: allows to turn on/off the torch lamp
- Geocoding: allowing geocoding operations
- Geolocation: allows you to retrieve the geographic location of the phone
- Gyroscope: allows monitoring the gyroscopic sensors of the device
- Magnetometer: allows to monitor the sensors of the magnetometer
- Open Browser: allows links to be opened in the phone's default browser
- Phone Dialer: allows an app to make a phone call
- Preferences: allows you to store user preferences in a key/value store
- Screen Lock: allows you to lock the screen
- Secure Storage: allows for secure storage of single key/value pairs.
- SMS: allows an application to open the default SMS application
- Text-to-Speech: allows the use of the voice synthesizer
- Version Tracking: lets you know if the application is launched for the first time, or if it is the first time for the current version ...
- Vibrate: allows you to release vibrations.
The supported platforms are:
- iOS (10+)
- Android (4.4+)
- UWP (Fall Creators Update+)
Note that they are also working to support Tizen.
How to use Xamarin.Essentials?
Install the Xamarin.Essentials NuGet package.
At the time of writing, Xamarin has not released a stable version yet; the current version is 0.6.
Reading the battery status
Recover the battery level:
var niveau = Battery.ChargeLevel; // chiffre compris entre 0.0 et 1.0. Retourne – 1 si inconnu
Recovery of the battery:
var etat = Battery.State;</pre>
<pre>
Attention on Android you'll need to add the following directive for this to work.
[assembly: UsesPermission(Android.Manifest.Permission.Battery)]
Read and write in preference
Recover value in preferences:
var myValue = Preferences.Get("ma_clef", "valeur_par_defaut");
Define a value:
Preferences.Set("ma_clef ", "ma_valeur");
Remove a key:
Preferences.Remove("ma_clef");
Delete all preferences:
Preferences.Clear();
Storing one securely
Record the value in the secure store:
await SecureStorage.SetAsync("oauth_token", "secret-oauth-token-value");
Recover value in the safe store:
var oauthToken = await SecureStorage.GetAsync("oauth_token");
To conclude:
The Xamarin teams are really trying to simplify the lives of cross platform developers.
To go further:
- The project's GitHub site: https://github.com/xamarin/Essentials
- Develop your cross-platform apps for iOS, Android and Windows