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

别等时光非礼了梦想. 提交于 2019-12-12 06:25:38

问题


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


来源:https://stackoverflow.com/questions/40295219/xamarin-forms-zxing-net-mobile-loosing-current-page-after-scan-result-on-ios-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!