Skip to content

Instantly share code, notes, and snippets.

@Eilon
Created January 31, 2020 22:59
Show Gist options
  • Save Eilon/85941d2b62ae74a0caab772a07346a79 to your computer and use it in GitHub Desktop.
Save Eilon/85941d2b62ae74a0caab772a07346a79 to your computer and use it in GitHub Desktop.
Mobile Blazor Bindings app using Xamarin.Essentials to get device info
<Frame CornerRadius="10" BackgroundColor="Color.LightBlue">
<StackLayout>
<StackLayout Orientation="StackOrientation.Horizontal">
<ProgressBar Progress="Battery.ChargeLevel" HeightRequest="20" HorizontalOptions="LayoutOptions.FillAndExpand" />
<Label Text="@($"{Battery.ChargeLevel.ToString("P")}")" />
</StackLayout>
<Label Text="@($"🔋 state: {Battery.State.ToString()}")" />
<Label Text="@($"🔋 source: {Battery.PowerSource.ToString()}")" />
<Button Text="Where am I?" OnClick="@WhereAmI" />
</StackLayout>
</Frame>
@code
{
async Task WhereAmI()
{
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium);
var location = await Geolocation.GetLocationAsync(request);
if (location != null)
{
var message = $"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}";
await Application.Current.MainPage.DisplayAlert("Found me!", message, "OK");
}
}
catch (FeatureNotSupportedException)
{
// Handle not supported on device exception
}
catch (FeatureNotEnabledException)
{
// Handle not enabled on device exception
}
catch (PermissionException)
{
// Handle permission exception
}
catch (Exception)
{
// Unable to get location
}
}
}
@sambhavyadav90
Copy link

found markup element with unexpected name 'ProgressBar'
The name 'Battery' does not exist in the current context

@Eilon
Copy link
Author

Eilon commented Feb 4, 2020

Hi @sambhavyadav90 the new components are not available on NuGet yet. You'll need to build the source in the repo to use the new components. We're working on an updated release to come out very soon that will have this all built.

Also, to access the Battery type, you'll need to use the Xamarin.Essentials library, which gives access to things like battery, geolocation, phone, SMS, email, and other device capabilities.

I'll be publishing lots more info on this hopefully later this week on the ASP.NET blog here: https://devblogs.microsoft.com/aspnet/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment