Introduction
I am going to show you how you can get the status if the battery is charged, discharged and the specific amount of percentage. It will show you how you can help users with just a few lines of code and ensure that you respect their device’s battery life. It is also useful to notify the battery status before your app performs any battery intensive operation performed by asking plug-in charge when the battery is low and no power supplied.
Create a new Windows 10 project and design using the following XAML code,
Let’s start
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <Grid.RowDefinitions>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- </Grid.RowDefinitions>
- <TextBlock x:Name="batteryStatusbox" HorizontalAlignment="Center" Grid.Row="0" VerticalAlignment="Center">
- </TextBlock>
- <ProgressBar x:Name="statusinPer" HorizontalAlignment="Center" Height="40" Grid.Row="1" VerticalAlignment="Top" Width="360" SmallChange="0.1" />
- </Grid>
Now go to code-behind page
- public sealed partial class MainPage: Page
- {
- public MainPage()
- {
- this.InitializeComponent();
- Windows.Devices.Power.Battery.AggregateBattery.ReportUpdated += AggregateBatteryOnReportUpdated;
- }
- private async void AggregateBatteryOnReportUpdated(Windows.Devices.Power.Battery sender, object args)
- {
- await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
- {
- // await UpdatePercentage(sender);
- var details = sender.GetReport();
- var getPercentage = (details.RemainingCapacityInMilliwattHours.Value / (double) details.FullChargeCapacityInMilliwattHours.Value);
- var getStatus = details.Status;
- string per = getPercentage.ToString("##%");
- statusinPer.Value = getPercentage;
- batteryStatusbox.Text = "Status: " + getStatus + "-Percentage :" + per;
- });
- }
- }
- string per = getPercentage.ToString("##%");
Subscribe the Event handler ReportUpdated event-triggered automatically when there are any changes in charge, capacity, or status.


Summary
In this article, we learned about Detecting And Get The Battery Status In Windows 10 UWP App.

Aditya GoudPosted May 4, 2017, 8:48 AM
This Works ! But only when the battery percentage is changed. Until the event docent get fired!
Harshad PansuriyaPosted Dec 12, 2015, 12:55 AM
Nice one