2

I'm using ZXing.Mobile.Forms to scan the barcodes. I would like to turn the torch on while scanning the barcodes. I tried ToggleTorch() but i dont see the torch light. Please help to overcome this scenario. Here is my code:

        var scanner = new ZXing.Mobile.MobileBarcodeScanner();
        scanner.ToggleTorch();
        var option = new ZXing.Mobile.MobileBarcodeScanningOptions { UseCode39ExtendedMode = true, TryHarder = true, PureBarcode = true, };
        var result = await scanner.Scan(option);
        if (result != null)
            await Application.Current.MainPage.DisplayAlert(title, result.Text, "Cancel");
        await Application.Current.MainPage.Navigation.PopAsync(true);
Ajith
  • 55
  • 11

1 Answers1

2

OK here is the main idea which does what you want, in an MVVM manner:

XAML:

<zxing:ZXingScannerView x:Name="ScannerView"
                        IsTorchOn="{Binding IsTorchOn}"
                        IsScanning="{Binding IsScanning}"
                        IsAnalyzing="{Binding IsAnalyzing}"
                        ScanResultCommand="{Binding OnScanResult}"/>

Code-behind:

public partial class BarcodeScannerPage
    {
        private BarcodeScannerPageModel _pageModel;

        public BarcodeScannerPage()
        {
            InitializeComponent();
        }

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

            if(_pageModel == null) return;
            _pageModel.IsScanning = true;
            _pageModel.IsAnalyzing = true;
            _pageModel.IsTorchOn= true;
        }

        protected override void OnBindingContextChanged()
        {
            base.OnBindingContextChanged();
            _pageModel = BindingContext as BarcodeScannerPageModel;
        }
    }

Page model:

public class BarcodeScannerPageModel
    {
        #region instance variables
        private bool _isScanning;
        private bool _isAnalyzing;
        private bool _isTorchOn;
        #endregion

        public BarcodeScannerPageModel()
        {
            IsTorchOn = true;
        }

        public bool IsScanning
        {
            get => _isScanning;
            set
            {
                _isScanning = value;
                RaisePropertyChanged();
            }
        }

        public bool IsAnalyzing
        {
            get => _isAnalyzing;
            set
            {
                _isAnalyzing = value;
                RaisePropertyChanged();
            }
        }

        public ICommand OnScanResult
        {
            get
            {
                return new Command(async (result) =>
                {
                        if (result.ToString().IsNullOrEmpty()) return;

                        Device.BeginInvokeOnMainThread(async () =>
                        {
                            IsAnalyzing = false;
                            //your code here...
                        });
                });
            }
        }

        public bool IsTorchOn
        {
            get => _isTorchOn;
            set
            {
                _isTorchOn = value;
                RaisePropertyChanged();
            }
        }
    }

Here I assumed MVVM is set and used correctly including "PropertyChanged" events and setting "BindingContext". More info:

MVVM

From Data Bindings to MVVM

MVVM & Data Binding with Xamarin.Forms

Using some MVVM frameworks such as FreshMvvm can make things easier.

VahidShir
  • 2,066
  • 2
  • 17
  • 27
  • It is opening the scanning screen, and scans the barcode and giving me the result – Ajith Jun 04 '18 at 13:24
  • xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms" is it the namespace, am i right? – Ajith Jun 04 '18 at 13:25