在Xaml中显示地图之前如何请求位置权限。 Xamarin.Forms [c#]


Answers

Question

我最近在Xamarin.forms中将sdk等级更新为6.0。 我使用Xaml在一个页面上放置一张地图。 由于我更新到6.0权限是需要显示地图。 我现在的问题是我无法弄清楚如何请求在应用程序试图显示它之前显示地图的权限。 结果我得到一个未处理的异常。

 public MapPage()
        {
            Init();
            InitializeComponent();


            azureService = AzureService.defaultManager;

        }
        private async Task Init()
        {
            await RequestLocationPermission();
        }
        protected async override void OnAppearing()
        {
            base.OnAppearing();

            MyMap.MoveToRegion(
               MapSpan.FromCenterAndRadius(
                   new Position(0, 0),
                   Distance.FromMiles(10.0)));

        }

private async Task RequestLocationPermission()
        {
            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)
                {
                                        }
                else if (status != PermissionStatus.Unknown)
                {
                    await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
                }
            }
            catch (Exception ex)
            {

            }
        }

如果地图是在Xaml中设置的,我怎么才能在显示之前请求许可?