Felix D.
Felix D.

Reputation: 5093

How to await my barcode scanning correctly

Currently I am struggling to await the reading of my QR-Code:

private async Task<string> ScanQRCode()
    {
        try
        {
            var options = new MobileBarcodeScanningOptions
            {
                AutoRotate = false,
                UseFrontCameraIfAvailable = false,
                TryHarder = true,
                DelayBetweenContinuousScans = 1
            };
            var scanPage = new ZXingScannerPage(options)
            {
                DefaultOverlayTopText = "Align the barcode within the frame",
                DefaultOverlayBottomText = string.Empty,
                DefaultOverlayShowFlashButton = true
            };

            string barcodestring = string.Empty;

            // Navigate to our scanner page
            Device.BeginInvokeOnMainThread(async () =>
            {
                await _navigation.PushModalAsync(scanPage);
            });

            scanPage.OnScanResult += (result) =>
            {
                if (result.Text.IsValidJson<DeviceSetup>())
                {
                    // Stop scanning
                    scanPage.IsScanning = false;

                    // Pop the page and show the result
                    Device.BeginInvokeOnMainThread(async () =>
                    {
                        await _navigation.PopModalAsync(true);
                    });
                    barcodestring = result.Text;
                }
            };

            return barcodestring;
        }
        catch (System.Exception ex)
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                await _navigation.PopModalAsync(true);
                await _page.DisplayAlert("Error", ex.Message, "OK");
            });
            return "Error";
        }
    }

The calling method continues execution and does not await the QR-Result ..

Why is that ?

Upvotes: 1

Views: 1124

Answers (1)

Alessandro Caliaro
Alessandro Caliaro

Reputation: 5768

I think your problem is that your

await _navigation.PushModalAsync(scanPage)

ends when the scanPage is visualize. So your

return barcodestring;

I think returns always an empty string.

You should use a MessagingCenter

            if (result.Text.IsValidJson<DeviceSetup>())
            {
                // Stop scanning
                scanPage.IsScanning = false;

                // Pop the page and show the result
                Device.BeginInvokeOnMainThread(async () =>
                {
                    await _navigation.PopModalAsync(true);
                });

                // Here send a message
                MessagingCenter.Send<MyPage, string>(this, "BarcodeRead", result.Text);
                //barcodestring = result.Text;
            }

And your MyPage should have (in OnAppearing) something like

MessagingCenter.Subscribe<MyPage, string> (this, "BarcodeRead", (sender, arg) => {
    // arg should have your barcode...
});

I have created a little demo TestZXing. It's a remake of demo present on https://github.com/Redth/ZXing.Net.Mobile

Upvotes: 1

Related Questions