繁体   English   中英

Xamarin形成ZXing QR码错误

[英]Xamarin Forms ZXing QR Code Error

我正在使用Xamarin表单制作QR码阅读器应用程序。 我已经找到了ZXing的实现,但是由于在async函数之外使用await关键字,因此在运行代码时出现错误。 本教程以这种方式执行此操作,但是我不知道自己在做错什么以抛出错误。

using Xamarin.Forms;
using ZXing.Net.Mobile.Forms;

namespace App3
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            var scanPage = new ZXingScannerPage();
            scanPage.OnScanResult += (result) => {
                // Stop scanning
                scanPage.IsScanning = false;

                // Pop the page and show the result
                Device.BeginInvokeOnMainThread(() => {
                    Navigation.PopAsync();
                    DisplayAlert("Scanned Barcode", result.Text, "OK");
                });
            };

            // Navigate to our scanner page
             await Navigation.PushAsync(scanPage); // Here is the error
        }
    }  
}

错误是:

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'

这是因为构造函数不能异步。 只需将代码移动到void方法,如:

private async void InitializeScanner()
{
   var scanPage = new ZXingScannerPage();
        scanPage.OnScanResult += (result) => {
            // Stop scanning
            scanPage.IsScanning = false;

            // Pop the page and show the result
            Device.BeginInvokeOnMainThread(() => {
                Navigation.PopAsync();
                DisplayAlert("Scanned Barcode", result.Text, "OK");
            });
        };

        // Navigate to our scanner page
         await pushAsyncPage(scanPage); 
}

public MainPage()
{
   InitializeComponent();
   InitializeScanner();
}

另一个更好的选择(进行一些调整,例如,在按钮cllick上打开扫描仪页面)可能是OnAppearing方法中的创建扫描页面,但是在扫描完成时要小心, Navigation.PopAsync()被调用,并且MainPage上的OnAppearing被调用。 因此,在这种情况下,将向上推新的扫描页面。

该消息是因为您需要在运行您的方法的外部方法中包括async关键字。 您遇到的问题是您试图在Page构造函数上运行它,并且它们不能是异步的。

您可以摆脱错误消息,将pushAsyncPage方法调用从构造函数中移出到页面中的另一个方法(例如OnAppearing并更改此添加异步的签名,例如:

    protected override async void OnAppearing ()
    {
        base.OnAppearing ();

        if(isPageLoaded)
            return;

        isPageLoaded = true;
        await pushAsyncPage(scanPage);
    }

或将整个代码块移至相同的方法:

    protected override async void OnAppearing ()
    {
        base.OnAppearing ();

        if(isPageLoaded)
            return;

        isPageLoaded = true;

        var scanPage = new ZXingScannerPage();
        scanPage.OnScanResult += (result) => {
            // Stop scanning
            scanPage.IsScanning = false;

            // Pop the page and show the result
            Device.BeginInvokeOnMainThread(() => {
                Navigation.PopAsync();
                DisplayAlert("Scanned Barcode", result.Text, "OK");
            });
        };

        // Navigate to our scanner page
         await pushAsyncPage(scanPage); // Here is the error            
    }

这应该足够了。

UPDATE

如下所述,使用此代码将需要有一个变量来知道页面是否已加载,以防止ZXing页面从扫描仪返回时再次显示。

这就是为什么我更喜欢在用户迭代(轻按按钮,滑动或任何其他手势)上打开扫描页面以防止此类循环的原因。

祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM