【发布时间】:2021-08-06 05:52:39
【问题描述】:
我尝试使用 Xamarin.Forms 在我的应用中使用地图功能。
在这部分代码中,我获得了允许在地图中查看我的位置的权限。
[Obsolete]
private async void GetPermession()
{
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse);
//var status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationWhenInUsePermission>();
if (status != Plugin.Permissions.Abstractions.PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse))
{
await
DisplayAlert("Need your location", "We need to acces your location", "Ok");
}
var result = await CrossPermissions.Current.RequestPermissionsAsync(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse);
if (result.ContainsKey(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse))
status = result[Plugin.Permissions.Abstractions.Permission.LocationWhenInUse];
}
if (status == Plugin.Permissions.Abstractions.PermissionStatus.Granted)
this.LocationMap.IsShowingUser = true;
else
await
DisplayAlert("Need your location", "We need to acces your location", "Ok");
}
catch (Exception exception)
{
await
DisplayAlert("Error", exception.Message, "Ok");
}
}
Visual Studio 告诉我
CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse)) 已过时,新方法为:
CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync()
但是,如果我使用新方法
var result = await CrossPermissions.Current.RequestPermissionsAsync(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse);
if (result.ContainsKey(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse))
status = result[Plugin.Permissions.Abstractions.Permission.LocationWhenInUse];
结果没有.ContainsKey 方法。
我该如何解决这个问题?
对不起,我的英语不好,谢谢
【问题讨论】:
-
你应该使用 Xamarin Essentials Permissions
标签: c# xml google-maps xamarin xamarin.android