Пример #1
0
        public ZXingCustomScanPage(ZXingScanOverlay overlay = null) : base()
        {
            _overlay = overlay ?? new ZXingScanOverlay();

            Title = "扫一扫";

            _zxing = new ZXingScannerView
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions   = LayoutOptions.FillAndExpand,
                AutomationId      = "zxingScannerView",
            };

            // 返回结果
            _zxing.OnScanResult += (result) =>
                                   Device.BeginInvokeOnMainThread(async() =>
            {
                _zxing.IsAnalyzing = false;

                await Navigation.PopModalAsync();    //安卓可能报错

                OnScanResult?.Invoke(result);
            });

            // 闪光灯
            _overlay.Options.FlashTappedAction = () =>
            {
                _zxing.IsTorchOn = !_zxing.IsTorchOn;
            };

            var grid = new Grid
            {
                VerticalOptions   = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
            };

            grid.Children.Add(_zxing);
            grid.Children.Add(_overlay);

            Content = grid;
        }
        private async void CustomScanBarCodeBtn_OnClicked(object sender, EventArgs e)
        {
            if (await CheckPerssion())
            {
                var options = new ZXingScanOverlayOptions()
                {
                    ScanColor = Color.Green,
                    ShowFlash = true
                };
                var overlay = new ZXingScanOverlay(options);
                var csPage  = new ZXingCustomScanPage(overlay);

                csPage.OnScanResult = (result) =>
                {
                    if (null != result)
                    {
                        CustomScanResult.Text = result.Text;
                    }
                };

                await Navigation.PushModalAsync(csPage);
            }
        }