Xamarin.Forms ZXing.Net.Mobile loosing current pag

2019-09-15 18:45发布

问题:

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..!! :)

回答1:

Every time MyTapScan.Tapped is called you are subscribing to MyScannerPage.OnScanResult so if you tap button 5 times your OnScanResult will be called 5 times. I hope now you know how to solve that.

One of possible solutions: Take your OnScanResult delegate and make it separate function, let say ScanFinished. Then instead of

MyScannerPage.OnScanResult += (result)

do

MyScannerPage.OnScanResult -= ScanFinished; 
MyScannerPage.OnScanResult += ScanFinished; 

Then you can be sure the event unsubscribed before you subscribe to it again



回答2:

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);
        };