Xamarin权限插件RequestPermissionsAsync不返回 [英] Xamarin permissions plugin RequestPermissionsAsync not returning

查看:511
本文介绍了Xamarin权限插件RequestPermissionsAsync不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试在创建者github帐户中为此打开门票,但他一直在不确认问题已解决的情况下一直关闭它.

I've tried opening tickets for this in the creators github account but he just kept closing it without confirmation that the issue was resolved.

我正在尝试实现插件,用于xamarin表单权限,除一个插件外,其他一切正常事情是,当我尝试使用真实设备请求许可时,任务永远不会返回用户是否允许或拒绝该请求.当我重新启动项目时,我可以看到用户关于权限的最后一项操作已生效,但是在请求时,该函数只是挂起,我无法根据用户选择的内容进行决策.下面是一个代码段

I'm trying to implement this plugin for xamarin forms permissions and everything works well except for one thing, when I try to request for permission using a real device, the task never returns whether or not the user allowed or denied the request. When I restart the project I can see that the user's last action regarding the permission took effect, however at the moment of the request, the function just hangs and I can't make decisions based on what the user selected. Below is a code snippet

var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
                if (status != PermissionStatus.Granted)
                {
                    //This line never returns
                    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
                    status = results[Permission.Location];
                }

也许这里的任何人都能阐明一切?任何帮助,将不胜感激.谢谢.

Perhaps anyone here can shed any light? Any help would be appreciated. Thank you.

推荐答案

我已经有一段时间解决此问题了.您可以按照以下步骤来确保完美安装.

I've got this issue for sometime and fixed it. You can follow these steps to insure a perfect installation.

在NuGet解决方案管理器中:-

  1. 为Android安装NuGet Plugin.CurrentActivity .
  2. 为Android安装NuGet Xamarin.Android.SupportAnnotations .
  3. 为所有项目安装NUGet Plugin.Permissions .
  1. Install NuGet Plugin.CurrentActivity for Android.
  2. Install NuGet Xamarin.Android.SupportAnnotations for Android.
  3. Install NUGet Plugin.Permissions for all projects.

在Android MainActivity.cs中:-

  1. protected override void OnCreate方法下的base.OnCreate(bundle/savedInstanceState);行之后添加此行CrossCurrentActivity.Current.Activity = this;.

  1. Add this line CrossCurrentActivity.Current.Activity = this; after the line base.OnCreate(bundle/savedInstanceState); under the protected override void OnCreate method.

添加此方法-如果存在,则将其覆盖:

Add this method -Or overwrite it if exists:

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

  1. 解决所有缺少的名称空间.

现在,您应该已经完成​​了完美的安装,请重试您的代码,它现在应该可以正常工作了.

By now you should have a perfect installation, go and retry your code, it should work now.

这篇关于Xamarin权限插件RequestPermissionsAsync不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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