【问题标题】:Geolocation GetLastKnownLocationAsync() permission exception from Xamarin Forms - call fails and no permission prompt来自 Xamarin Forms 的 Geolocation GetLastKnownLocationAsync() 权限异常 - 调用失败并且没有权限提示
【发布时间】:2019-02-16 02:57:27
【问题描述】:

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

我有最新的 Xamarin.Essentials nuget 包。

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

am 从我的 ViewModel 调用它。

调用超级简单:

var location = await Geolocation.GetLastKnownLocationAsync();

但即使我的 info.plist 已正确设置,它也失败并且未能提示用户权限对话框: NSLocationWhenInUseUsageDescription 插入原因

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

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

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

【问题讨论】:

    标签: xamarin.forms geolocation xamarin.essentials


    【解决方案1】:

    这个答案的灵感来自 Xamarin/Azure 布道者 Brandon Minnick --> 看看他的项目,他用下面的 code: 处理类似情况

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

    因此,为了采用上述代码解决问题中解决的地理位置问题,我做了以下操作:

    Task<Xamarin.Essentials.Location> GetLocationFromPhone()
    {
    
        var locationTaskCompletionSource = new TaskCompletionSource<Xamarin.Essentials.Location>();
    
        Device.BeginInvokeOnMainThread(async () =>
        {
            locationTaskCompletionSource.SetResult(await Geolocation.GetLastKnownLocationAsync());
        });
    
        return locationTaskCompletionSource.Task;
    }
    

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

    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)
            {
            }
        }
    }
    

    希望对其他人有帮助!

    【讨论】:

    • 你可以标记你的答案,让更多人看到。
    【解决方案2】:

    如果您使用的是 Xamarin.Essentials 并且未在 Android 上提示您授予权限,请确保您已将所有必要的代码添加到 Android Main Activity。

    详情请见https://docs.microsoft.com/en-us/xamarin/essentials/get-started?tabs=windows%2Candroid

    来自文档:

    protected override void OnCreate(Bundle savedInstanceState) {
        //...
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState); // add this line to your code, it may also be called: bundle
        //...
    

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-08
      • 1970-01-01
      • 2016-12-28
      • 2023-01-17
      • 2016-03-18
      • 2018-04-26
      • 1970-01-01
      • 2012-08-27
      • 2016-09-27
      相关资源
      最近更新 更多