Xamarin Xzing条码扫描器重新扫描

如何解决Xamarin Xzing条码扫描器重新扫描

我正在使用xamarin android形式的zxing条形码扫描仪,我可以将其扫描到一个条形码而没有任何问题,但是我希望能够丢弃他们进行的扫描并能够进行另一次扫描

我也在使用MVVM。这是我的xaml ...

 <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
          
            <forms:ZXingScannerView  x:Name="zxingView" 
                                     IsTorchOn="{Binding TorchON}"       
                                     IsScanning="{Binding IsScanning}" 
                                     IsAnalyzing="{Binding IsAnalyzing}"
                                     Result="{Binding Result,Mode=TwoWay}" 
                                     ScanResultCommand="{Binding ScanCommand}"
                                     />
            <forms:ZXingDefaultOverlay               
                x:Name="scannerOverlay"                                                       
                BottomText="Place the red line over the barcode you'd like to scan." />
            <Button Grid.Row="1" Text="Toggle Flash"  Command="{Binding FlashToggleCommand}"></Button>
        </Grid>

这是我的页面模型

private string barcode = string.Empty;

    public string Barcode
    {
        get { return barcode; }
        set { barcode = value; }
    }

    private bool _isAnalyzing = true;

    public bool IsAnalyzing
    {
        get { return _isAnalyzing; }
        set
        {
            if (!Equals(_isAnalyzing,value))
            {
                _isAnalyzing = value;
                OnPropertyChanged("IsAnalyzing");
            }
        }
    }

    private bool _isScanning = true;
    private bool _torchON = false;
    private DynamicContainerPageModel _hhtScreen;
    private readonly IDeviceManager _deviceManager;

    public ScanningViewPageModel(IDeviceManager deviceManager)
    {
        _deviceManager = deviceManager;
    }

    public override void Init(object initData)
    {
        base.Init(initData);
        _hhtScreen = initData as DynamicContainerPageModel;

    }


    public bool IsScanning
    {
        get { return _isScanning; }
        set
        {
            if (!Equals(_isScanning,value))
            {
                _isScanning = value;
                OnPropertyChanged("IsScanning");
            }
        }
    }

    public bool TorchON
    {
        set
        {
            if (_torchON != value)
            {
                _torchON = value;
                OnPropertyChanged("TorchON");
            }
        }
        get { return _torchON; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
    }

    public Command ScanCommand
    {
        get
        {
            return new Command(() =>

            {
                IsAnalyzing = false;
                IsScanning = false;

                Device.BeginInvokeOnMainThread(async () =>
                {
                    Barcode = Result.Text;
                    var response = await CoreMethods.DisplayAlert("Barcode found","Found: " + Result.Text,"Keep","Scan Again");

                    if (response)
                    {
                        //Save the value into the model
                        _deviceManager.BeginInvokeOnMainThread(() =>
                                    {
                                        _hhtScreen.SelectedControl.Text = barcode;
                                    });

                        //close page
                        await this.CoreMethods.PopPageModel(false);
                    }
                    else
                    {
                        Result = null;
                        IsAnalyzing = true;
                        IsScanning = true;
                    }
                });

                IsAnalyzing = true;
                IsScanning = true;
            });
        }
    }

    public Command FlashToggleCommand
    {
        get { return new Command(async () => { TorchON = !TorchON; }); }
    }

    public Result Result { get; set; }

当我再次在弹出窗口中按下扫描时,我发现它有点被击中,并且错过了扫描相机是否再次激活的机会,大多数情况下它只是冻结。难道我做错了什么?是否有更好的方法让控件重新扫描?

解决方法

过去我遇到了一个非常相似的问题。我最终要做的是在后面的代码中定义Scanner视图,然后根据需要有条件地从视图中添加/删除该视图。 这就是我的最终样子:

XAML:

    <!--
        The barcode scanner grid. The actual barcode scanner is
        created and added to the grid in the code behind class.
    -->
    <Grid x:Name="ScannerViewGrid"
          Grid.Row="3"
          HorizontalOptions="FillAndExpand"
          IsVisible="{Binding IsBarcodeScannerRunning}"
          VerticalOptions="FillAndExpand" />

背后的代码:

private ZXingDefaultOverlay scannerOverlay;
private ZXingScannerView scannerView;

private void CreateNewScannerView()
{
    var vm = BindingContext.DataContext as SearchViewModel;

    ScannerViewGrid.Children.Clear();

    scannerOverlay = null;
    scannerView = null;

    scannerOverlay = new ZXingDefaultOverlay();
    scannerOverlay.ShowFlashButton = false;

    scannerView = new ZXingScannerView();
    scannerView.SetBinding(ZXingScannerView.ResultProperty,nameof(vm.BarcodeScanResult),BindingMode.OneWayToSource);
    scannerView.SetBinding(ZXingScannerView.ScanResultCommandProperty,nameof(vm.BarcodeScanResultCommand));
    scannerView.IsScanning = true;

    ScannerViewGrid.Children.Add(scannerView);
    ScannerViewGrid.Children.Add(scannerOverlay);
}

private void RemoveScannerView()
{
    ScannerViewGrid.Children.Clear();

    scannerView.IsScanning = false;
    scannerView.IsAnalyzing = false;
    scannerView.RemoveBinding(ZXingScannerView.ResultProperty);
    scannerView.RemoveBinding(ZXingScannerView.ScanResultCommandProperty);

    scannerView = null;
    scannerOverlay = null;
}
,

我如何解决这个问题:

Xaml

<coreControls:ContainerLayout.Content>
            <Grid>
                <ContentView
                    x:Name="contentViewCamera"/>
                <coreControls:SvgImage
                    SvgSource="img_qr_background"
                    VerticalOptions="FillAndExpand"
                    Aspect="AspectFill"/>
                <Grid
                    x:Name="mainLayout"
                    RowDefinitions="48,*,*">
                    <contentViews:HeaderView Title="Canjear cupon"
                                             TitleColor="{AppThemeBinding Light={StaticResource LightWhiteColor},Dark={StaticResource DarkWhiteColor}}"
                                             RightIconSvg="ic_flash_w"
                                             Margin="6,0" />
                    <material:MaterialEntry
                        Grid.Row="3"
                        HorizontalOptions="Center"
                        VerticalOptions="Center"
                        WidthRequest="240"
                        HeightRequest="42"
                        BackgroundColor="#70000000"
                        BorderColor="#70000000"
                        Placeholder="Ingresa el codigo"/>
                </Grid>
            </Grid>
        </coreControls:ContainerLayout.Content>

背后的代码

public partial class RedeemCouponPage
{
    private ZXingScannerView _scannerView;

    public RedeemCouponPage()
    {
        InitializeComponent();
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();

        //mainLayout.Padding = PageHelper.GetPageSafeArea(new Thickness(0,20),this);
    }
    protected override void OnViewModelSet()
    {
        base.OnViewModelSet();
        var options = new MobileBarcodeScanningOptions
        {
            AutoRotate = false,TryHarder = true,TryInverted = true,DelayBetweenAnalyzingFrames = 5,DelayBetweenContinuousScans = 5,PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE }
        };

        ViewModel.InitializeCameraCommand = new MvxCommand(() =>
        {
            _scannerView = new ZXingScannerView()
            {
                Options = options,ScanResultCommand = ViewModel.ScanResultCommand
            };

            _scannerView.SetBinding(ZXingScannerView.IsScanningProperty,nameof(ViewModel.IsBusy));
            _scannerView.SetBinding(ZXingScannerView.IsAnalyzingProperty,nameof(ViewModel.IsBusy));
            _scannerView.SetBinding(ZXingScannerView.IsTorchOnProperty,nameof(ViewModel.IsFlashActive));
            _scannerView.SetBinding(ZXingScannerView.ResultProperty,nameof(ViewModel.Result),BindingMode.TwoWay);
            contentViewCamera.Content = _scannerView;
        });
    }
}

视图模型

public class RedeemCouponViewModel: BaseViewModel
{
    private readonly ICouponService CouponService = Mvx.IoCProvider.Resolve<ICouponService>();

    public IMvxCommand InitializeCameraCommand { get; set; }

    private ZXing.Result _result;
    public ZXing.Result Result
    {
        get => _result;
        set
        {
            SetProperty(ref _result,value);
            BarCodeText = value.Text;
        }
    }

    private bool _isFlashActive;
    public bool IsFlashActive
    {
        get => _isFlashActive;
        set => SetProperty(ref _isFlashActive,value);
    }

    private string _barCodeText;
    public string BarCodeText
    {
        get => _barCodeText;
        set => SetProperty(ref _barCodeText,value);
    }

    private Coupon _coupon;
    public Coupon Coupon
    {
        get => _coupon;
        set => SetProperty(ref _coupon,value);
    }

    public override void ViewAppeared()
    {
        base.ViewAppeared();

        if (DeviceInfo.DeviceType == DeviceType.Physical)
            InitializeCameraCommand.Execute();

        IsBusy = true;
    }

    public IMvxAsyncCommand ScanResultCommand => new MvxAsyncCommand(async () =>
    {
        if (Result == null)
        {
            IsBusy = true;
            return;
        }
        else
            IsBusy = false;

        Coupon = await CouponService.Get(BarCodeText);
        PopupIsVisible = true;


    },() => IsBusy);

    public IMvxCommand ConfirmRedeemCommand => new MvxCommand(()=>
    {
        DisplaySuccessView("Canjeado!","El cupon ha sido canjeado con exito.");
        if (DeviceInfo.DeviceType == DeviceType.Physical)
            InitializeCameraCommand.Execute();
        PopupIsVisible = false;
        IsBusy = true;
    });

    public IMvxCommand BackToScanerCommand => new MvxCommand(() =>
    {
        PopupIsVisible = false;
        if (DeviceInfo.DeviceType == DeviceType.Physical)
            InitializeCameraCommand.Execute();

        IsBusy = true;
    });
}
,

尝试创建新页面然后销毁它。如果您在同一活动(页面)上,则更难轻松实现重新扫描。

这个链接https://www.c-sharpcorner.com/blogs/qrcode-barcode-scanner-and-generator-for-xamarin-forms-using-zxing-nuget对我帮助很大。

祝您有美好的一天。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-