Search code examples
iosxamarin.iosxamarin.androidxamarin.formszxing

Xamarin.Forms ZXing.Net.Mobile loosing current page after scan result on iOS 10


I'm using Xamarin.Forms and I have implemented ZXing.Net.Mobile for scanning bar codes.

On Android it's working fine, on iOS 10 after reading a barcode the function "OnScanResult" is fired and executes the command Navigation.PopAsync() which closes the scanning page but after a second it closes also the current page where I have displayed the result !

        MyTapScan.Tapped += async (sender, e) =>
        {                
            await MyBtScan.ScaleTo(1.20, 100, Easing.Linear);             
            await MyBtScan.ScaleTo(1, 100, Easing.Linear);
            await Task.Delay(50);
            //--------------------------------------------
            MyAppLib.MyAppUtilitiesBarCodeReader MyBarCodeReader = new MyAppLib.MyAppUtilitiesBarCodeReader();                                
            var MyScannerPage = MyBarCodeReader.GetBarCodeReaderPage();
            //--------------------------------------------
            MyScannerPage.OnScanResult += (result) => {
                //Stop scanning
                MyScannerPage.IsScanning = false;
                //Pop the page and show the result
                Device.BeginInvokeOnMainThread(() => {
                    Navigation.PopAsync();
                    MyMachSerialNumber.Text = result.Text;
                });
            };
            //--------------------------------------------
            //Display scanner
            await Navigation.PushAsync(MyScannerPage);
        };

Please please help..!! :)


Solution

  • I have introduced a new variable to check if the scanning was already fired and now it's working fine and as expected. This is the code:

    MyTapScan.Tapped += async (sender, e) =>
            {                
                await MyBtScan.ScaleTo(1.20, 100, Easing.Linear);             
                await MyBtScan.ScaleTo(1, 100, Easing.Linear);
                await Task.Delay(50);
                bool MyIsScanning = true;
                //--------------------------------------------
                MyAppLib.MyAppUtilitiesBarCodeReader MyBarCodeReader = new MyAppLib.MyAppUtilitiesBarCodeReader();                                
                var MyScannerPage = MyBarCodeReader.GetBarCodeReaderPage();
                //--------------------------------------------
                MyScannerPage.OnScanResult += (result) => {
                    //Stop scanning
                    MyScannerPage.IsScanning = false;
                    //Pop the page and show the result
                    Device.BeginInvokeOnMainThread(() => {
                        if (MyIsScanning == true)
                        {
                            MyIsScanning = false;
                            MyMachSerialNumber.Text = result.Text;
                            Navigation.PopAsync();
                        }                                                
                    });
                };
                //--------------------------------------------
                //Display scanner
                await Navigation.PushAsync(MyScannerPage);
            };