尽管获得了camara在Android中的给定权限,但获得了扫描仪的白屏

问题描述 投票:0回答:1

以下是我在xamarin c#中扫描qr代码所实现的代码

ZXingScannerView zxing= new ZXingScannerView();
  private void btnScan_Clicked(object sender, EventArgs e)
        {
            try
            {
                zxing = new ZXingScannerView {
                    HorizontalOptions = LayoutOptions.CenterAndExpand,
                    VerticalOptions = LayoutOptions.CenterAndExpand,
                    BackgroundColor = Color.Orange
                };
                NavigationPage.SetHasNavigationBar(this, false);
                zxing.OnScanResult += (result) =>
                {
                    Device.BeginInvokeOnMainThread(async () =>
                    {
                        zxing.IsAnalyzing = false;
                        zxing.IsScanning = false;
                        //MessagingCenter.Send(result, "Scanresult");
                        if (result != null)
                        {
                            await DisplayAlert("Scanned code: ", text, "OK");
                        }
                    });
                };

                var layout = new StackLayout();
                layout.BackgroundColor = Color.Red;
                layout.Children.Add(zxing);

                Content = zxing;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

此外,我已在androidmanifest.xml中授予了摄像头的权限。这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.companyname.xxxxxx" android:installLocation="auto" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
    <uses-permission android:name="android.permission.CAMERA" />
    <application android:label="xxxxxx.Android"></application>
    <application android:name="android.support.multidex.MultiDexApplication"></application>
</manifest>

在此之前,我尝试实现以下代码:

 private async void btnScan_Clicked(object sender, EventArgs e)
        {
            try
            {
                ZXingScannerPage scanPage = new ZXingScannerPage();
                //await Navigation.PushAsync(scanPage);
               scanPage.OnScanResult += (result) =>
               {
                   // Stop scanning
                   scanPage.IsScanning = false;

                   // Pop the page and show the result
                   Device.BeginInvokeOnMainThread(async () =>
               {
                   await Navigation.PopAsync();
                   if (result != null)
                   {                      
                       await DisplayAlert("Scanned code: ", text, "OK");
                   }
               });
               };
               await Navigation.PushAsync(scanPage);
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

但是所有代码在android 5上都可以正常工作,但在android 8.1上都无法正常工作,如果我缺少某些东西,谁能帮助找到我?

c# xamarin xamarin.android
1个回答
0
投票

但是所有代码都可以在android 5上正常工作,但没有一个在android 8.1上正常工作

Android 6.0]起,应用必须始终执行运行时权限检查。在MainAcitivity类中请求应用程序所需的权限。

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

查看教程:

https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows#requesting-system-permissions

热门问题
推荐问题
最新问题