Пример #1
0
        public void ScanContinuously(MobileBarcodeScanningOptions options, Action <Result> scanHandler)
        {
            try
            {
                options.TryInverted   = true;
                _continousScanHandler = scanHandler;
                UIDevice.CurrentDevice.InvokeOnMainThread(() => {
                    _qrCodeScanController = new QrCodeScanController(options, this)
                    {
                        ContinuousScanning = true
                    };
                    _qrCodeScanController.OnScannedResult += barcodeResult => {
                        if (barcodeResult == null)
                        {
                            ((UIViewController)_qrCodeScanController).InvokeOnMainThread(() => {
                                ((UIViewController)_qrCodeScanController).DismissViewController(true, null);
                            });
                        }

                        scanHandler(barcodeResult);
                    };

                    ViewHelper.CurrentController.PresentViewController((UIViewController)_qrCodeScanController, true, null);
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Пример #2
0
        public Task <Result> Scan(MobileBarcodeScanningOptions options)
        {
            options.TryInverted = true;
            return(Task.Factory.StartNew(() => {
                try
                {
                    _scanResultResetEvent.Reset();

                    Result result = null;
                    UIDevice.CurrentDevice.InvokeOnMainThread(() => {
                        _qrCodeScanController = new QrCodeScanController(options, this);
                        _qrCodeScanController.OnScannedResult += barcodeResult => {
                            ((UIViewController)_qrCodeScanController).InvokeOnMainThread(() => {
                                _qrCodeScanController.Cancel();

                                try
                                {
                                    ((UIViewController)_qrCodeScanController).DismissViewController(true, () => {
                                        result = barcodeResult;
                                        _scanResultResetEvent.Set();
                                    });
                                }
                                catch (ObjectDisposedException)
                                {
                                    result = barcodeResult;
                                    _scanResultResetEvent.Set();
                                }
                            });
                        };

                        ViewHelper.CurrentController.PresentViewController((UIViewController)_qrCodeScanController, true, null);
                    });

                    _scanResultResetEvent.WaitOne();
                    ((UIViewController)_qrCodeScanController).Dispose();

                    return result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }
            }));
        }