我尝试使用Xamarin在我的应用程序中使用地图功能。表格。
在代码的这一部分中,我获得了允许在地图中查看我的位置的权限。
[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告诉我
交叉权限。现在的ShouldShowRequestPermissionRationaleAsync(Plugin.Permissions.Abstractions.Permission.LocationWhenUse((已过时,新方法为:
交叉权限。现在的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];
结果不具有CCD_ 1方法。
我该如何解决这个问题?
抱歉我英语不好,谢谢
您可以使用Xamarin获取权限。像这样的基本和依赖服务:
接口
public interface IPermissionHelper
{
Task<PermissionStatus> RequestPermissionsAsync();
}
从共享项目调用:
await DependencyService.Get<IPermissionHelper>().RequestPermissionsAsync();
iOS渲染器:
public class PermissionHelper : IPermissionHelper
{
public PermissionHelper()
{
}
/// <summary>
/// Defines the showTrackingMap.
/// </summary>
LocationCheck showTrackingMap;
/// <summary>
/// The RequestPermissionsAsync.
/// </summary>
/// /// <returns>The <see cref="Task<PermissionStatus>"/>.</returns>
public async Task<PermissionStatus> RequestPermissionsAsync()
{
PermissionStatus permission = PermissionStatus.Unknown;
showTrackingMap = new LocationCheck((s, ev) => {
if ((ev as LocationCheck.LocationCheckEventArgs).Allowed)
{
permission = PermissionStatus.Granted;
}
else
{
permission = PermissionStatus.Denied;
}
showTrackingMap.Dispose();
});
while (permission == PermissionStatus.Unknown)
{
await Task.Delay(10);
}
return permission;
}
}
public class LocationCheck : NSObject, ICLLocationManagerDelegate
{
public class LocationCheckEventArgs : EventArgs
{
public readonly bool Allowed;
public LocationCheckEventArgs(bool Allowed)
{
this.Allowed = Allowed;
}
}
CLLocationManager locationManager;
EventHandler locationStatus;
public LocationCheck(EventHandler locationStatus)
{
this.locationStatus = locationStatus;
Initialize();
}
public LocationCheck(NSObjectFlag x) : base(x) { Initialize(); }
public LocationCheck(IntPtr handle) : base(handle) { Initialize(); }
public LocationCheck(IntPtr handle, bool alloced) : base(handle, alloced) { Initialize(); }
public void Initialize()
{
locationManager = new CLLocationManager
{
Delegate = this
};
locationManager.RequestWhenInUseAuthorization();
}
[Export("locationManager:didChangeAuthorizationStatus:")]
public void AuthorizationChanged(CLLocationManager manager, CLAuthorizationStatus status)
{
switch (status)
{
case CLAuthorizationStatus.AuthorizedAlways:
case CLAuthorizationStatus.AuthorizedWhenInUse:
locationStatus.Invoke(locationManager, new LocationCheckEventArgs(true));
break;
case CLAuthorizationStatus.Denied:
case CLAuthorizationStatus.Restricted:
locationStatus.Invoke(locationManager, new LocationCheckEventArgs(false));
break;
}
}
protected override void Dispose(bool disposing)
{
locationStatus = null;
locationManager.Delegate = null;
locationManager.Dispose();
base.Dispose(disposing);
}
}
public async void GetPermission()
{
var permission = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
if (permission != PermissionStatus.Granted)
{
permission = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}
else if(permission == PermissionStatus.Granted)
this.LocationMap.IsShowingUser = true;
if (permission != PermissionStatus.Granted)
return;
}
我下定决心,用这种方法,把所有人都交给