我正在使用Xamarin.Forms,并且已经实现了ZXing.Net.Mobile来扫描条形码。

在Android上运行正常,在iOS 10上读取条形码后,功能“ OnScanResult”被激发并执行命令Navigation.PopAsync(),它关闭了扫描页面,但一秒钟后,它也关闭了我显示结果的当前页面!

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


请帮忙.. !! :)

最佳答案

每次调用MyTapScan.Tapped时,您都在订阅MyScannerPage.OnScanResult,因此,如果单击5次按钮,则将调用OnScanResult 5次。我希望现在您知道如何解决该问题。

可能的解决方案之一:
以您的OnScanResult委托并将其设为单独的功能,例如说ScanFinished。然后代替

MyScannerPage.OnScanResult += (result)



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


然后,您可以确保取消订阅该事件,然后再次订阅

08-08 06:09