Webview 和 QR Scanner 通过 webview Android c# Visual Studio 2019 上的按钮

问题描述

我在 VS2019 中有一个 android webview 应用程序。我正在尝试调用一个 JS 函数,该函数调用一个 JS 接口并打开 QR 扫描仪。我无法做到的通过远程URL到达JS接口代码...

这是我的代码...

MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Webkit;
using Vintasoft.XamarinBarcode;
using ZXing.Mobile;
using java.lang;
using System.Threading.Tasks;
using Java.Interop;
using Android.Content;

namespace XFAS_Android
{
    [Activity(Label = "XFAS Delivery",Icon = "@drawable/icon",MainLauncher = true)]
    public class MainActivity : Activity
    {
        WebView _webview;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this,savedInstanceState);
            // Set our view from the "main" layout resource

            MobileBarcodeScanner.Initialize(Application);
            SetContentView(Resource.Layout.activity_main);

            _webview = FindViewById<WebView>(Resource.Id.webview);
            _webview.SetWebViewClient(new WebViewClient());
            _webview.Settings.JavaScriptEnabled = true;
            _webview.Settings.AllowFileAccessFromFileURLs = true;
            _webview.Settings.AllowUniversalAccessFromFileURLs = true;
            _webview.Settings.AllowFileAccess = true;
            _webview.Settings.AllowContentAccess = true;
            _webview.Settings.DomStorageEnabled = true;

            _webview.AddJavascriptInterface(new QRScannerJSInterface(_webview),"QRScannerJSInterface");

            _webview.LoadUrl("https://m.priorityconnections.in");

            Button btnScan = FindViewById<Button>(Resource.Id.btnScan);
            btnScan.Click += Button_Click;
        }

        private void Button_Click(object sender,System.EventArgs e)
        {

        }

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

            base.OnRequestPermissionsResult(requestCode,grantResults);
        }

        public class QRScannerJSInterface : java.lang.Object
        {
            QRScanner qrScanner;
            WebView webView;

            public QRScannerJSInterface(WebView webView)
            {
                this.webView = webView;
                qrScanner = new QRScanner();
            }

            [Export("ScanQR")]
            public void ScanQR()
            {
                qrScanner.ScanQR()
                    .ContinueWith((t) =>
                    {
                    if (t.Status == TaskStatus.RanToCompletion)
                            webView.LoadUrl(@"javascript:getQRValue('" + t.Result + "')");
                    });
            }
        }
    }
}

QRScanner.cs

namespace XFAS_Android
{
    class QRScanner
    {
        MobileBarcodeScanner scanner;

        public QRScanner()
        {
            scanner = new MobileBarcodeScanner();
        }

        public async Task<string> ScanQR()
        {
            scanner.UseCustomOverlay = false;
            scanner.TopText = "Scanning for barcode";
            var result = await scanner.Scan();
            return result.ToString();
        }
    }
}

远程 URL 扫描按钮代码...

function scan() {
            try
            {
                QRScannerJSInterface.ScanQR();
            }
            catch(err) {
                ShowRedMsgLarge('Error',err.message);
            }
        };

调用 QRScannerJSInterface.ScanQR 时,我总是收到一条警告,说函数未定义。 需要帮助。

代码灵感来自... Launching a QR scanner from Android web view and returning the scanned result using Xamarin

在这里做错了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)