The Powerclass is available at Xamarin.Essentials API. It provides information about the device's energy-saver status, which indicates if the device is running in a low-power mode. Applications should avoid background processing if the device's energy-saver status is on. Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C#, leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.
Reading this article, you can learn how to find the device's power energy-saver status in Xamarin Forms applications using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in cross platform application development.
The following important tools are required for developing Xamarin Forms App,
- Windows 10 (Recommended)
- Visual Studio 2017
- Android API 19 or higher and UWP 10.0.16299.0 or higher.
Now we can discuss step by step app development.
Step 1
Open Visual studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give the Suitable Name for your App (XamFormPowerStatus) ->OK
Step 2
Select the Cross Platform template as Blank APP ->Set Platform as Android and UWP and code sharing strategy as .NET standard. Afterwards, Visual Studio creates 3 projects (Portable, Android, UWP).
Step 3
For adding reference, right click your solution (XamFormPowerStatus) and select Manage Nuget Packages.
For adding Xamarin.Essentials Reference, choose Browse and search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install.
Step 4
Add the Label controls in Mainpage.Xaml for displaying Title and Energy saver status
- <Label FontAttributes="Bold" Text=" Find the device Power Energy-Saver status in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>
- <Label x:Name="lblpwrsts" ></Label>
Step 5
Add the following code in the MainActivity.cs in XamFormPowerStatus.Android project,
In onCreate() method,
- Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Also add the below method,
- publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {
- Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- }
Step 6
Add the following namespace and code in Mainpage.Xaml.cs
- using Xamarin.Essentials;
- public MainPage() {
- InitializeComponent();
- lblpwrsts.Text = "Energy Saver Status is : " + Power.EnergySaverStatus.ToString();
- Power.EnergySaverStatusChanged += OnEnergySaverStatusChanged;
- }
- privatevoid OnEnergySaverStatusChanged(object sender, EnergySaverStatusChangedEventArgs e) {
-
- lblpwrsts.Text = "Energy Saver Status is : " + e.EnergySaverStatus.ToString();
- }
Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormPowerstatus" x:Class="XamFormPowerstatus.MainPage">
- <StackLayout>
- <Label FontAttributes="Bold" Text=" Find the device Power Energy-Saver status in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />
- <Label x:Name="lblpwrsts"></Label>
- </StackLayout>
- </ContentPage>
Step 7
Deploy your App in UWP and Android. The output of the XamFormPowerStatus App is, before energy saver is On,
After Energy saver is on,
Summary
Now you have successfully verified the device's power energy-saver status in a Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.