C# Xamarin ZXing Unable to Scan Barcode after Change IsScanning Property from False to True

330 Views Asked by At

I have a barcode scannner page, namely BarcodeScannerView and BarcodeScannerViewModel. The main objective of this is to navigate to another page after barcode is read. What I had done is the application success navigate to other page after scanned a barcode. Before navigate to next page, I had set IsScanning = false to prevent next scan. However, when I try to return back to the barcode scanner page, I had set the property IsScanning = true but the scanner does not work anymore. Any idea on how to solve this?

BarcodeScannerView

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MultiPosApp.InventorySystem.Views.BarcodeScannerView"
         NavigationPage.HasNavigationBar="False"
         xmlns:prism="http://prismlibrary.com"
         prism:ViewModelLocator.AutowireViewModel="True"
         xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
         xmlns:zxcm="clr-namespace:ZXing.Common;assembly=zxing.portable">


<Grid RowSpacing="0" ColumnSpacing="0" BackgroundColor="Transparent" Margin="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
    <zxing:ZXingScannerView x:Name="scanner" Grid.RowSpan="3" Grid.ColumnSpan="3" 
                            ScanResultCommand="{Binding ScanResultCommand}" 
                            Result="{Binding BarcodeResult, Mode=TwoWay}"
                            IsScanning="{Binding IsScanning, Mode=TwoWay}"
                            IsAnalyzing="{Binding IsAnalyzing, Mode=TwoWay}"
                            VerticalOptions="CenterAndExpand"
                            HorizontalOptions="CenterAndExpand"/>

    <zxing:ZXingDefaultOverlay  Grid.RowSpan="3" 
                                Grid.ColumnSpan="3"                                                                
                                BottomText="Place the red line over the barcode you'd like to scan."  />
    
</Grid>
</ContentPage>

BarcodeScannerViewModel

public class BarcodeScannerViewModel : ViewModelBase
{
    #region Variables
    private INavigationService m_NavigationService;
    private IEventAggregator m_EventAggregator;
    private SQLDbConnection Conn;

    public DelegateCommand ScanResultCommand { get; set; }

    private ZXing.Result m_BarcodeResult;
    public ZXing.Result BarcodeResult
    {
        get { return m_BarcodeResult; }
        set { SetProperty(ref m_BarcodeResult, value); }
    }

    private bool m_IsScanning = true;
    public bool IsScanning
    {
        get { return m_IsScanning; }
        set { SetProperty(ref m_IsScanning, value); }
    }

    private bool m_IsAnalyzing = true;
    public bool IsAnalyzing
    {
        get { return m_IsAnalyzing; }
        set { SetProperty(ref m_IsAnalyzing, value); }
    }

    private string m_Barcode;
    public string Barcode
    {
        get { return m_Barcode; }
        set { SetProperty(ref m_Barcode, value); }
    }
    #endregion

    #region Constructor
    public BarcodeScannerViewModel(INavigationService navigationService, IEventAggregator eventAggregator, SQLDbConnection sql) : base(navigationService)
    {
        m_NavigationService = navigationService;
        m_EventAggregator = eventAggregator;
        Conn = sql;

        ScanResultCommand = new DelegateCommand(OnScanResult);
    }
    #endregion


    #region Method
    private void OnScanResult()
    {
        IsAnalyzing = false;
        IsScanning = false;

        Device.BeginInvokeOnMainThread(async () =>
        {
            
                string barcode = BarcodeResult.Text;
                await m_NavigationService.NavigateAsync("RegisterGoodsView");

                IsAnalyzing = true;
                IsScanning = true;  
        });
    }

    public override void OnNavigatedTo(INavigationParameters parameters)
    {
        Device.BeginInvokeOnMainThread(async () =>
        {
            IsAnalyzing = true;
            IsScanning = true;
        });
    }
    #endregion
}
1

There are 1 best solutions below

0
On

However, when I try to return back to the barcode scanner page, I had set the property IsScanning = true but the scanner does not work anymore.

Firstly, you need to confirm that you use the same BarcodeScannerViewModel instance Before or after you natigate to the page.

Then you need to implement INotifyPropertyChanged interface to notify IsScanning value changed.

public class ViewModelBase : INotifyPropertyChanged
{
    
    public event PropertyChangedEventHandler PropertyChanged;
  
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

private bool m_IsScanning = true;
    public bool IsScanning
    {
        get { return m_IsScanning; }
        set
        {
            m_IsScanning = value;
            RaisePropertyChanged("IsScanning");
        }
    }