Xamarin Forms 中的 Geolocation GetLastKnownLocationAsync() 权限异常 - 调用失败且没有权限提示 [英] Geolocation GetLastKnownLocationAsync() permission exception from Xamarin Forms - call fails and no permission prompt

查看:34
本文介绍了Xamarin Forms 中的 Geolocation GetLastKnownLocationAsync() 权限异常 - 调用失败且没有权限提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GetLocationAsync 在我的 Xamarin.Forms 应用上失败.

GetLocationAsync fails on my Xamarin.Forms app.

我有最新的 Xamarin.Essentials nuget 包.

I've got the latest Xamarin.Essentials nuget package.

我已经在 info.plist 中设置了必要的权限.

I've set the necessary permissions in the info.plist.

正在从我的 ViewModel 调用它.

I am calling this from my ViewModel.

调用非常简单:

var location = await Geolocation.GetLastKnownLocationAsync();

但它既失败又无法提示用户权限对话框,即使我的 info.plist 已正确设置:NSLocationWhenInUseUsageDescription插入原因

but it's both failing AND failing to prompt a user permission dialog even though my info.plist has been setup correctly with: NSLocationWhenInUseUsageDescription Insert reason

我提出并回答这个问题是因为它令人头疼,而且我不确定要搜索什么或问题是什么.

I'm asking and answering this question because it was a head scratcher, and I wasn't exactly sure what to be searching for or what the issue was.

我的各种搜索都指出了许多相关问题,但实际上没有找到主要问题.

My various searches pointed to many related issues but nothing that actually gets to the main problem.

我得到的最接近的实际上是 Essentials github 页面上的这个问题:https://github.com/xamarin/Essentials/issues/634

The closest I got was actually this issue on the Essentials github page: https://github.com/xamarin/Essentials/issues/634

推荐答案

这个答案的灵感来自 Xamarin/Azure 布道者 Brandon Minnick --> 看看他的项目,他用以下 代码:

This answer is inspired by Xamarin/Azure evangelist, Brandon Minnick --> take a look at his project where he handles a similar situation with the following code:

那么我们可以从上面得到什么?如果你看一下上下文,他已经以 MVVM 风格将他的 Views 与他的 ViewModels 连接起来.但是,各种库要求从主线程调用某些方法.这就是问题的本质,这就是这段代码可以解决的问题.

So what can we take away from the above? If you look at the context, he has connected his Views with his ViewModels in MVVM style. However, various libraries require that certain methods be called from the Main thread. This is the essence of the issue, and this is what this code can solve.

因此,为了针对问题中解决的地理定位问题采用上述代码,我执行了以下操作:

So to adopt the above code for the geolocation issue addressed in the question, I did the following:

Task<Xamarin.Essentials.Location> GetLocationFromPhone()
{

    var locationTaskCompletionSource = new TaskCompletionSource<Xamarin.Essentials.Location>();

    Device.BeginInvokeOnMainThread(async () =>
    {
        locationTaskCompletionSource.SetResult(await Geolocation.GetLastKnownLocationAsync());
    });

    return locationTaskCompletionSource.Task;
}

我在任务中使用我的 ViewModel 中的上述内容.类似于以下内容.

I'm using the above from my ViewModel from within a Task. Something like the following.

async Task ExecuteGetGeoLocationCommand()
{
    try
    {
        var locationFromPhone = await GetLocationFromPhone().ConfigureAwait(false);

        if (locationFromPhone is null)
            return;

        _location = locationFromPhone;

        if (_location != null)
        {
            Console.WriteLine($"Latitude: {_location.Latitude}, Longitude {_location.Longitude}, Altitude: {_location.Altitude}");
        } 
        else
        {
            Console.WriteLine($"Exiting geolocation");
        }
        catch (FeatureNotSupportedException fnsEx)
        {
        }
        catch (Exception ex)
        {
        }
    }
}

希望对其他人有帮助!

这篇关于Xamarin Forms 中的 Geolocation GetLastKnownLocationAsync() 权限异常 - 调用失败且没有权限提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆