3

Resoution TL;DR : https://gist.github.com/rupe120/78f8a57f0ed7ecacbdc13fa2da8d931a


I created my own scan page, converting the built-in ZXingScannerPage code (https://github.com/Redth/ZXing.Net.Mobile/blob/master/Source/ZXing.Net.Mobile.Forms/ZXingScannerPage.cs) to a Page + PageModel/View concept. The page code is below.

The problem is that OnScanResult is never triggered.

I was using ZXingScannerPage directly previously, and the OnScanResult event was successfully triggering but I wanted the page to follow the same format as the rest of the application. So the QR code I'm using should trigger it.

I must be missing a setup piece in the ZXingScannerView, but I can't see it.

Any thoughts?

SearchQrPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.Pages.SearchQrPage"
             xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms">
    <ContentPage.Content>
        <Grid>
            <zxing:ZXingScannerView x:Name="scannerView" />
            <zxing:ZXingDefaultOverlay x:Name="scannerOverlay"
                                       TopText="Hold your phone up to the QR code"
                                       BottomText="Scanning will happen automatically"
                                       ShowFlashButton="True"/>
        </Grid>
    </ContentPage.Content>
</ContentPage>

SearchQrPage.xaml.cs

using MyApp.PageModels;
using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace MyApp.Pages
{
    public partial class SearchQrPage : ContentPage
    {

        public SearchQrPage()
        {
            InitializeComponent();

            scannerView.Options = new ZXing.Mobile.MobileBarcodeScanningOptions
            {
                PossibleFormats =
                new List<ZXing.BarcodeFormat>
                        {
                            ZXing.BarcodeFormat.QR_CODE
                        }
            };

            scannerView.OnScanResult += ScannerView_OnScanResult;

            scannerOverlay.FlashButtonClicked += ScannerOverlay_FlashButtonClicked;
        }

        private void ScannerOverlay_FlashButtonClicked(Button sender, EventArgs e)
        {
            scannerView.ToggleTorch();
        }

        private void ScannerView_OnScanResult(ZXing.Result result)
        {
            var model = this.BindingContext as SearchQrPageModel;
            if (model == null)
                return;

            scannerView.IsScanning = false;

            if (model.ScanResultCommand.CanExecute(result))
                model.ScanResultCommand.Execute(result);

        }
    }
}
Josh Russo
  • 3,080
  • 2
  • 41
  • 62
  • It doesnt hit any breakpoint you put in there? Is the `ScannerOverlay_FlashButtonClicked` working? – Gerald Versluis Jan 30 '18 at 15:22
  • Correct. It doesn't hit any breakpoints in `ScannerView_OnScanResult` but it does for `ScannerOverlay_FlashButtonClicked` – Josh Russo Jan 30 '18 at 15:27
  • 1
    I guess you simply didn't start to scan. Try set `scannerView.IsScanning = true;` at the end of the constructor. On the original ZXing's Page, they do it at OnAppearing event (btw, I'm sorry for my poor English) – Diego Rafael Souza Jan 30 '18 at 15:42
  • That was it! Thanks! If you post that as an answer I'll be sure to mark it as the accepted one – Josh Russo Jan 30 '18 at 16:22

1 Answers1

4

Just set IsScanning = true at the constructor, for example. On the original ZXing's page, they do it on the OnAppearing event.

You've missed it up.

Diego Rafael Souza
  • 5,241
  • 3
  • 23
  • 62