我正在创建一个应用程序,我正在尝试在我的Android应用程序中的设备(在“设置”中)中启用或不启用“位置”权限 . 我正在使用Xamarin.Forms . 我如何在Xamarin中通过C#获得这个?
使用GeoLocator插件并检查IsGeoLocationEnabled属性
var locator = CrossGeolocator.Current; if (locator.IsGeoLocationEnabled) { ... }
使用James montemagno的权限插件 . 可用here
特别是下面的代码片段将帮助您:
try { var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location); if (status != PermissionStatus.Granted) { if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location)) { await DisplayAlert("Need location", "Gunna need that location", "OK"); } var results = await CrossPermissions.Current.RequestPermissionsAsync(new [] { Permission.Location }); status = results[Permission.Location]; } if (status == PermissionStatus.Granted) { var results = await CrossGeolocator.Current.GetPositionAsync(10000); LabelGeolocation.Text = "Lat: " + results.Latitude + " Long: " + results.Longitude; } else if (status != PermissionStatus.Unknown) { await DisplayAlert("Location Denied", "Can not continue, try again.", "OK"); } } catch (Exception ex) { LabelGeolocation.Text = "Error: " + ex; }
2 回答
使用GeoLocator插件并检查IsGeoLocationEnabled属性
使用James montemagno的权限插件 . 可用here
特别是下面的代码片段将帮助您: