PlatformChecks.RequestPermissions不可用

时间:2019-01-26 09:10:07

标签: c# android visual-studio xamarin zxing

我正在使用xamamrin使用c#为android开发应用程序,我正在使用的IDE是Visual Studio。 我想在我的应用程序中使用zxing,所以我将zxing数据包添加到核心层和ui层。

如下面的代码所示,我可以实例化一些类,但是我无权访问

 PlatformChecks.IsPermissionInManifest

我需要它来检查天气是否授予了所需的权限。请看看我收到的错误消息

请让我知道如何使用它。

错误

Error CS0103: The name 'PlatformChecks' does not exist in the current context (CS0103) (MITScan.UI.Droid)

代码

 public static readonly string[] RequiredPermissions = new[] {
        Android.Manifest.Permission.Camera
    };
    protected override async void OnCreate(Android.OS.Bundle bundle)
    {
        base.OnCreate(bundle);
        this.Window.AddFlags(WindowManagerFlags.KeepScreenOn);
        SetContentView(Resource.Layout.ZxingLibTestView);
        _scannerFragment = new ZXingScannerFragment();
SupportFragmentManager.BeginTransaction().Replace(Resource.Id.tscan_4_view_relativelayout_cameraPreview, _scannerFragment, "Scannerfragment").Commit();

        var permissionsToRequest = new List<string>();

        // Check and request any permissions
        foreach (var permission in RequiredPermissions)
        {
            if (PlatformChecks.IsPermissionInManifest(this, 
  permission))
            {
                if (!PlatformChecks.IsPermissionGranted(this, 
  permission))
                    permissionsToRequest.Add(permission);
            }
        }

        if (permissionsToRequest.Any())
        {
            _waitingForPermission = 
  PlatformChecks.RequestPermissions(this, 
  permissionsToRequest.ToArray(), 101);
        } 

        }

注意

I am using Zxing for scanning bar codes.

1 个答案:

答案 0 :(得分:0)

在阅读了这个问题之后,我正在浏览Zebra Crossing's(ZXing)文档,因为我知道没有这样的Android类,并且发现PlatformChecks是如下的自定义类:

public class PlatformChecks
{
    public const string PERMISSION_CAMERA = "android.permission.CAMERA";
    public const string PERMISSION_FLASHLIGHT = "android.permission.FLASHLIGHT";

    public static bool HasCameraPermission(Context context)
    {
        return HasPermission (context, PERMISSION_CAMERA);
    }

    public static bool HasFlashlightPermission(Context context)
    {
        return HasPermission (context, PERMISSION_FLASHLIGHT);
    }

    static bool HasPermission(Context context, string permission)
    {
        PermissionInfo pi = null;

        try { pi = context.PackageManager.GetPermissionInfo (PERMISSION_CAMERA, PackageInfoFlags.Permissions); }
        catch { }

        return pi != null;
    }
}

并且具有以下用途

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Content.PM;